oracle 存储函数 FUNCTION
–存储函数有返回值类型和返回值,存储过程没有返回值
案例1:比较两个数,并返回最大值
CREATE OR REPLACE FUNCTION maxnum(a int,b int) return int
AS
max1 number:=0;
BEGIN
if a>b then
max1:=a;
else
max1:=b;
end if;
RETURN max1;
END;
调用:
BEGIN
dbms_output.put_line(maxnum (5,10));
END;
存储函数一般在sql语句中使用:
select maxnum (5,10) from dual;
案例2:查询某个员工的年薪(包含月薪和奖金)
CREATE OR REPLACE FUNCTION queryyearsal(eno in number)
RETURN number
AS
--定义两个变量保存员工的薪水和奖金
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;
存储函数一般在sql语句中使用:
select queryyearsal(7369) from dual;