存储过程和存储函数

存储过程

存储过程(Stored Procedure)是在大型数据库系统中,一组为了完成特定功能的SQL语句集,经编译后存储在数据库中,用户通过指定存储过程的名字并给出参数(如果该存储过程带有参数)来执行它。存储过程是数据库中的一个重要对象,任何一个设计良好的数据库应用程序都应该用到存储过程。

创建存储过程用法:
create or replace PROCEDURE 过程名[(参数名 in/out 数据类型)]
As
begin
PLSQL子程序体;
End;
或者
create or replace PROCEDURE 过程名[(参数名 in/out 数据类型)]
is
begin
PLSQL程序体;
End 过程名;
例子: 给指定的员工涨100工资,并打印出涨前和涨后的工资。

create or replace procedure addSall(eno in number) 
    is
        pemp myemp%rowtype;
    begin
        select * into pemp from myemp where empno = eno;
        update  myemp set sal = sal + 100 where empno = eno;
    dbms_output.put_line('涨工资前'||pemp.sal||'涨工资后'||(pemp.sal+100);
    end addSall;

结果
这里写图片描述

存储函数

create or replace function 函数名(Name in type, Name out type,…)
return 数据类型
is
结果变量 数据类型;
begin
return (结果变量);
end 函数名;
存储过程和存储函数的区别
一般来讲,过程和函数的区别在于函数可以有一个返回值;而过程没有返回值。
但过程和函数都可以通过out制定一个或多个输出参数。我们可以利用out参数,在过程和参数中实现返回多个值。
示例:使用存储函数来查询指定员工的年薪

create or replace function empincome(eno in emp.empno%type)
    return number 
is
    psal emp.sal%type;
    pcomm emp.comm%type;
begin
    select t.sal into psal from emp t where T.EMPNO = eno;
    return psal*12+nvl(pcomm,0);
end;

使用存储过程来替换上面的例子

create or replace procedure empincomep(eno in emp.empno%type,income out number) 
is
    psal emp.sal%type;
    pcomm emp.comm%type;
begin
    select t.sal into psal from emp t where T.EMPNO = eno;
    income:=psal*12+nvl(pcomm,0);
end empincomep;

调用:

declare 
    income number;
begin
    empincomep(7639,income);
    dbms_output.put_line(income);
end;
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值