Oracle 存储函数

存储过程(存储函数):指存储在数据库中供所有用户程序调用的子程序(PL/SQL程序)。
存储过程没有返回值,存储函数有返回值(return语句,要有返回值的类型)。
区别:存储函数可以有返回值,而存储过程没有返回值。

一般原则:如果只有一个返回值,用存储函数;否则用存储过程。

存储函数:

--查询某个员工的年收入(月薪*12+奖金)

create or replace function queryEmpIncome(eno in number)   --eno是存储函数的形参,in表示输入参数。
return number   --存储函数有返回值
is
       --定义变量保存月薪和奖金
       psal emp.sal%type;
       pcomm emp.comm%type;
begin
       --得到月薪和奖金
       select sal,comm into psal,pcomm from emp where empno=eno; 
       
       --返回年收入
       return psal*12+nvl(pcomm,0);

end queryEmpIncome;
/

存储函数的调用:

--调用存储函数
begin
  :result := queryEmpIncome(7839);
end;

通过PL/SQL Developer工具创建存储函数:

通过PL/SQL Developer工具调用存储函数:

猜你喜欢

转载自blog.csdn.net/houyanhua1/article/details/82425482