加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

【3】Oracle_存储函数

发布时间:2020-12-12 15:40:22 所属栏目:百科 来源:网络整理
导读:1、语法 createorreplace function函数名 (参数1in|out类型,参数2in|out类型)retrun结果类型 as|is --定义变量 begin return变量 ;--(变量的类型一定是跟return的结果类型保持一致) end; 2、应用 (1)声明fun_emp_totalsal存储函数,查询指定员工的年薪

1、语法

createorreplacefunction函数名(参数1in|out类型,参数2in|out类型)retrun结果类型 as|is --定义变量 begin return变量;--(变量的类型一定是跟return的结果类型保持一致) end;

2、应用

(1)声明fun_emp_totalsal存储函数,查询指定员工的年薪
create or replace functionfun_emp_totalsal(eno number) return number as psal number; begin select sal*12+nvl(comm,0) into psal from emp where empno = eno; return psal; end;
调用: declare totalSal number; begin totalSal :=fun_emp_totalsal(7788); dbms_output.put_line(totalSal); end; 运行:
(2)声明pro_emp_totalsal存储过程,查询指定员工的年薪
createorreplaceprocedurepro_emp_totalsal(enonumber,totalsaloutnumber) as begin selectsal*12+nvl(comm,0)intototalsalfromempwhereempno=eno; end;

调用和运行和上面的存储函数一样。
(3)声明fun_emp_dname存储函数,根据部门编号查询出部门名称
createorreplacefunctionfun_emp_dname(dnonumber)returndept.dname%type as pnamedept.dname%type; begin selectdnameintopname fromdeptwheredeptno=dno; returnpname; end;

调用--查询编号为10的部门名称 declare pnamedept.dname%type; begin pname:=fun_emp_dname(10); dbms_output.put_line(pname); ;


(4)在select语句中调用存储函数
selectempno,ename,fun_emp_dname(deptno)fromemp;

3、存储过程和存储函数的区别:

  • 语法不一样
  • 存储函数必须有返回值
  • 存储过程虽然没有返回值,但是可以指定输出参数类型
  • 存储函数可以在select中使用

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读