【2】Oracle_存储过程

1、定义:

将提前编译好的一段plsql,存放到数据库段,供其他程序员调用

2、语法:

    create [or replace] procedure 过程名称(参数1 in|out 类型)
    as|is 
            --声明一些变量
    begin
      
    end;   

3、案例:

(1)声明无返回值pro_add_sal存储过程  作用:给指定员工涨100工资,并打印涨前和涨后工资
create or replace procedure pro_add_sal(eno in number)
as
  psal number;
begin
  select sal into psal from emp where empno = eno;
  dbms_output.put_line('涨前工资'||psal);
  update emp set sal = sal + 100 where empno = eno;
  commit;
  dbms_output.put_line('涨后工资'||(psal+100));
end;  
调用该存储过程:
begin
  pro_add_sal(7788);
end;


(2)声明有返回值pro_emp_sal存储过程 作用:查询指定员工的年薪
create or replace procedure pro_emp_totalsal(eno number,totalsal out number)
as
begin
   select sal*12+nvl(comm,0into totalsal from emp where empno = eno;
end;  
调用该存储过程:
declare
  total number;
begin
   pro_emp_totalsal(7788,total);
   dbms_output.put_line(total);
end; 
说明: 
    一般来讲,存储过程没有返回值,但是我们可以利用out参数,在过程中实现返回多个值。

(3)声明输出参数为游标类型pro_emplist  作用:输入部门编号,将此部门下的所有员工信息输出
create or replace procedure pro_emplist(dno number,emplist out sys_refcursor)
as
begin
   open emplist for select * from emp where deptno = dno;
end;  

调用该存储过程:
declare
  emplist sys_refcursor;
  pemp emp%rowtype;
begin
  pro_emplist(10,emplist);
     loop
        fetch emplist into pemp;
        exit when emplist%notfound;
        dbms_output.put_line(pemp.empno||pemp.ename); 
     end loop;
  close emplist;
end;

运行结果:
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值