示例13 函数创建和调用

来源:互联网 发布:中电科大数据院 编辑:程序博客网 时间:2024/06/02 03:32


/*============================================================
                       示例 函数
  ============================================================*/
create or replace function get_sal(eno number) return number
is
v_sal emp.sal%type;
begin
     select sal into v_sal from emp where empno=eno;
     return v_sal;
exception
         when no_data_found then
              raise_application_error(-20012,'该雇员不存在');
end;

--调用查询薪水函数
declare
    v_sal number;
    emp_20012 EXCEPTION;
    PRAGMA EXCEPTION_INIT(emp_20012, -20012);  
begin
    v_sal:=get_sal(7780);
    dbms_output.put_line(v_sal);
exception
    when emp_20012 then
       v_sal:=0; 
       dbms_output.put_line('该雇员不存在');
end;
/*============================================================
                       示例 函数
  ============================================================*/
create or replace function get_sal(eno number) return number
is
v_sal emp.sal%type;
begin
     select sal into v_sal from emp where empno=eno;
     return v_sal;
exception
         when no_data_found then
              raise_application_error(-20012,'该雇员不存在');
end;

--调用查询薪水函数
declare
    v_sal number;
    emp_20012 EXCEPTION;
    PRAGMA EXCEPTION_INIT(emp_20012, -20012);  
begin
    v_sal:=get_sal(7780);
    dbms_output.put_line(v_sal);
exception
    when emp_20012 then
       v_sal:=0; 
       dbms_output.put_line('该雇员不存在');
end;
0 0
原创粉丝点击