游标 包

--游标操作
declare
cursor c_sal is select empno,ename,sal from emp; begin for v_sal in c_sal loop dbms_output.put_line(v_sal.empno||' '||v_sal.ename||' '||v_sal.sal); end loop; end;
--包定义
CREATE
OR REPLACE PACKAGE pkg_emp AS minsal NUMBER; maxsal NUMBER; e_beyondbound EXCEPTION; PROCEDURE update_sal( p_empno NUMBER, p_sal NUMBER); PROCEDURE add_employee( p_empno NUMBER,p_sal NUMBER); END pkg_emp;
--包体
CREATE OR REPLACE PACKAGE BODY pkg_emp
AS
    PROCEDURE update_sal(p_empno NUMBER, p_sal NUMBER)
    AS
    BEGIN
      SELECT min(sal), max(sal) INTO minsal,maxsal FROM emp;
      IF p_sal BETWEEN minsal AND maxsal THEN
        UPDATE emp SET sal=p_sal WHERE empno=p_empno;
        IF SQL%NOTFOUND THEN
          RAISE_APPLICATION_ERROR(-20000,'The employee 
                                                                 doesn''t exist');
        END IF;
      ELSE
        RAISE e_beyondbound;
      END IF;
   EXCEPTION
      WHEN e_beyondbound THEN
         DBMS_OUTPUT.PUT_LINE('The salary is beyond bound! ');
    END update_sal;
  PROCEDURE add_employee(p_empno NUMBER,p_sal NUMBER)
AS
 BEGIN
      SELECT min(sal), max(sal) INTO minsal,maxsal FROM emp;
      IF p_sal BETWEEN minsal AND maxsal THEN
         INSERT INTO emp(empno,sal) VALUES(p_empno,p_sal);
      ELSE
         RAISE e_beyondbound;
      END IF;
  EXCEPTION
    WHEN e_beyondbound THEN
      DBMS_OUTPUT.PUT_LINE('The salary is beyond bound! ');
  END add_employee;
END pkg_emp;
--包的使用
BEGIN
  pkg_emp.update_sal(7844,3000);
  pkg_emp.add_employee(1111,4000);
END;

 

posted @ 2020-03-24 11:22  墨白95  阅读(149)  评论(0编辑  收藏  举报