存储过程

学习存储过程中时遇到了一些相应的例题和问题,在这里和大家分享一下,希望大家多多指正


使用存储过程,查询显示emp表中各部门中最长雇佣时间

使用游标for循环完成:
set serveroutput on;
   create or replace procedure my_pro1 as
   cursor cur_emp is select deptno,min(hiredate) minhiredate from emp group by deptno;
   begin
   for x in cur_emp loop
   dbms_output.put_line('部门号'||x.deptno||'  雇佣时间'||x.minhiredate);
   end loop;
   end;
   
 使用普通游标完成:  
   create or replace procedure my_pro1 as
   cursor cur_emp is select deptno ,min(hiredate) from emp group by deptno;
   var_deptno emp.deptno%type;
   var_hiredate emp.hiredate%type;
   begin
   open cur_emp;
   fetch cur_emp into var_deptno,var_hiredate;
   while cur_emp%found loop
   dbms_output.put_line('部门号'||var_deptno||'  雇佣时间'||var_hiredate);
   fetch cur_emp into var_deptno,var_hiredate;
   end loop;
   close cur_emp;
   end;
   
创建一个存储过程,以部门号为参数,查询该部门的平均工资,并输出该部门中比平均工资高的员工号,员工名


首先使用普通游标来查询
create or replace procedure my_pro1(var_deptno in emp.deptno%type) as
   cursor cur_emp is select empno,ename,sal from emp where deptno=var_deptno and sal>(select avg(sal) from emp where deptno=var_deptno);  /*复合查询语句*/
   type emp_record is record(
   var_empno emp.empno%type,
   var_ename emp.ename%type,
   var_sal emp.sal%type
   );
   var_sal emp.sal%type;
   x emp_record;
   begin
   select avg(sal) into var_sal from emp where deptno=var_deptno;
   dbms_output.put_line('该部门平均工资为'||var_sal);  /*输出该部门中的平均工资*/
   open cur_emp;
   fetch cur_emp into x;
   while cur_emp%found loop
   dbms_output.put_line('员工号'||x.var_empno||'员工姓名'||x.var_ename||'员工工资'||x.var_sal);
   fetch cur_emp into x;
   end loop;
   close cur_emp;
   end;


使用游标for循环完成
   create or replace procedure my_pro1(var_deptno in emp.deptno%type) as
   cursor emp_cur is select empno,ename,sal from emp where deptno=var_deptno and sal>(select avg(sal) from emp where deptno=var_deptno);
   begin
   for x in emp_cur loop
   dbms_output.put_line('员工号'||x.empno||' 员工姓名'||x.ename||' 员工工资'||x.sal);
   end loop;
   end;
   
   
调用存储过程
execute my_pro1(10);




使用存储过程,根据员工编号显示员工信息。如果是员工工资大于2500则显示员工名和工资,否则报错。
create or replace procedure querry_emp
(e_no in emp.empno%type,e_name out emp.ename%type,e_sal out emp.sal%type)
as
null_exception exception;
begin
select ename,sal into e_name,e_sal from emp where empno=e_no;
if e_sal>2500 then  dbms_output.put_line('员工姓名'||e_name||'  工资'||e_sal);
elsif e_sal<=2500
then raise null_exception;
end if;
exception
when null_exception
then  dbms_output.put_line('错误');
end;


调用存储过程
declare
 a1 emp.ename%type;
 a2 emp.sal%type;
 begin
 querry_emp(7788,:a1,:a2);
end;
in 需要提前赋值,out不需要提前赋值,只需要提供一个变量,在存储过程执行完的时候,output类型的变量值就会修改,获得返回值,只能通过这个方法获得返回值,而不能像函数一样直接返回结果。因为存储过程是没有返回值的
且通过该法可以获得多个返回值。


以上是在学习存储过程中遇到的一些例题与问题,欢迎大家指正。
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

彭祥.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值