ITPub博客

首页 > Linux操作系统 > Linux操作系统 > sqlplus函数的使用

sqlplus函数的使用

原创 Linux操作系统 作者:hit-2075 时间:2011-11-17 15:22:06 0 删除 编辑
----函数可以当一个变量一样使用,如果担心编译器的问题,可以函数付一变量,重复使用
--需要指明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博客 ” ,链接:https://blog.itpub.net/15681893/viewspace-711395/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: pl/sql异常处理
下一篇: IBM AIX系列培训
请登录后发表评论 登录
全部评论

注册时间:2011-09-18

  • 博文量
    28
  • 访问量
    44382