【存儲過程】輸入學生的姓名,打印出學生的最高分、最低分、平均分


 1 create or replace procedure showStudentInfo(ThisStudentName varchar2)
 2 as
 3   thismax number;
 4   thismin number;
 5   thisavg number;
 6   sno number;
 7   line student%rowtype;
 8 begin
 9   select sid into sno from student where sname=ThisStudentName;  --先求學號,檢測學號是否合法,不存在立即進入異常
10   select max(cmark),min(cmark),trunc(avg(cmark),2) into thismax,thismin,thisavg
11   from mark
12   where sid = sno;
13   dbms_output.put_line(ThisStudentName||'的最高分為:'||thismax||'最低分為:'||thismin||'平均分為:'||thisavg);
14 exception
15   when no_data_found then
16        dbms_output.put_line(ThisStudentName||'不存在,請核對!');
17   when too_many_rows then
18       for line in(select*from student where sname=ThisStudentName) loop      ---取出學生表的每一行  循環取出
19         select max(cmark),min(cmark),trunc(avg(cmark),2) into thismax,thismin,thisavg 
20         from mark
21         where sid=line.sid;
22          dbms_output.put_line(line.sid||ThisStudentName||'的最高分為:'||thismax||'最低分為:'||thismin||'平均分為:'||thisavg);
23       end loop;
24 end;

 

1 -----調用---
2 set serveroutput on
3 begin 
4   showStudentInfo('蕭瑾');
5 end;

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM