sqlplus函数的使用

----函数可以当一个变量一样使用,如果担心编译器的问题,可以函数付一变量,重复使用
--需要指明return类型
create or replace function check_sal return boolean is
       v_sal scott.emp.sal%type;
       v_deptno  scott.emp.deptno%type;
       v_empno   scott.emp.empno%type;
       v_avg_sal scott.emp.sal%type;
begin
       v_empno := 7782;
       select sal,deptno into v_sal,v_deptno from scott.emp where empno = v_empno;
      
       select avg(sal) into v_avg_sal from scott.emp where deptno = v_deptno;
      
       if v_sal > v_avg_sal then
--每一个结束分支都需要return
         return true;
       else
----每一个结束分支都需要return
         return false;
       end if;
exception
       when no_data_found then、
--每一个结束分支都需要return
         return null;
end;
-----------------------------------------
declare
  ret boolean;
begin
--付一变量避免重复执行函数
  ret := check_sal;
  if (ret is null) then
    dbms_output.put_line('The function returned NULL due to exception');
  elsif (ret) then
    dbms_output.put_line('salary > average');
  else
    dbms_output.put_line('salary < average');
  end if;
end;
---------------------------------------函数在sql中的调用需要注意
create or replace function dml_call_sql(p_sal number) return number is
begin
--不能包含ddl语句,sql执行的时候会报错
--如果sql执行时间过长,重复调用函数,可能引发读一致性问题
  --insert into emp(emp.empno,emp.ename,emp.hiredate,emp.sal,emp.deptno) values(1,'zhang',sysdate,p_sal,30);
  return (p_sal + 100);
end;
--------------------------------------
update emp set sal = dml_call_sql(2000) where empno = 7934;

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/15681893/viewspace-711395/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/15681893/viewspace-711395/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值