createorreplacefunction f_num_sal(vempno in emp.empno%type)return number
as
vsal number;beginselect sal into vsal from emp where empno=vempno;return vsal;end;/
createorreplaceprocedure f_deptno_avgdeptsal(vdeptno in emp.deptno%type)as
vsal number;beginselectavg(sal)into vsal from emp where deptno=vdeptno;
dbms_output.put_line('平均工资'||vsal);
dbms_output.put_line('比平均工资高的员工号、员工名');for v_emp in(select*from emp where deptno=vdeptno and sal>vsal)loop
dbms_output.put_line(v_emp.empno||'、'||v_emp.ename);endloop;end;
创建一个存储过程,以部门号为参数,返回该部门的人数和最高工资。
createorreplaceprocedure p_deptno_numandmaxsal(pdeptno emp.deptno%type)is
max_sal emp.sal%type;
a number;beginselectcount(*)into a from emp where deptno=pdeptno;
dbms_output.put_line('部门人数='||a);selectmax(sal)into max_sal from emp where deptno=pdeptno;
dbms_output.put_line('最高工资='||max_sal);end;
创建一个以部门号为参数,返回该部门最高工资的函数。
createorreplacefunction f_deptno_maxsal(vdeptno in emp.deptno%type)return number
as
max_sal number;beginselect sal into max_sal from emp where empno=vdeptno;return max_sal;end;
使用存储过程统计每个学生的‘已修学分’。
createtable stu(
sname char(10)notnull,
sno char(10)notnull,
tcredit number);createtable sc
(sno char(10)notnull,
cno char(10)notnull,
grade number,
credit number,primarykey(sno,cno));createtable course
(cno char(10)notnull,
cname char(20)notnull);insertinto sc values('100001','200001',69,5);insertinto sc values('100001','200002','50','4');insertinto course values('200002','数据库系统设计');createorreplaceprocedure p_student_credit(
p_name out stu.sname%type,
p_credit out number)asbeginfor p in(select a.sname,sum(b.credit) credit
from stu a,sc b
where a.sno=b.sno)loop
dbms_output.put_line(p.sname||'、'||p.credit);endloop;end;
使用触发器实现当登记学生成绩(60分以上)时自动统计学生的‘已修学分’。
createorreplacetrigger t_insert_credit
afterinserton sc
for each rowbeginif :new.grade>=60thenupdate stu
set stu.tcredit=stu.tcredit+ :new.credit where stu.sno= :new.sno;endif;end;
createorreplacefunction f_cno_num(f_cno sc.cno%type)return number
as
num number;
name char(20);beginselect cname into name from course where cno=f_cno;selectcount(*)into num from sc where grade<60and cno=f_cno;
dbms_output.put_line(name||'未及格人数'||num);return num;end;
select f_cno_num(200002) 不及格人数 from dual;
将上实验中的存储过程与本实验中函数创建包。
createorreplace package emp_package
isfunction f_num_sal(vempno in emp.empno%type)return number;procedure f_deptno_avgdeptsal(vdeptno in emp.deptno%type);procedure p_deptno_numandmaxsal(pdeptno emp.deptno%type);function f_deptno_maxsal(vdeptno in emp.deptno%type)return number;end emp_package;
创建一个函数,以员工号为参数,返回该员工的工资。create or replace function f_num_sal(vempno in emp.empno%type) return numberasvsal number;beginselect sal into vsal from emp where empno=vempno;return vsal;end;/创建...