1. 程式人生 > 實用技巧 >儲存過程-輸入學生的姓名,打印出學生的最高分、最低分、平均分

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

 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;