您好,登錄后才能下訂單哦!
--創建包規范
CREATE OR REPLACE PACKAGE first_package
IS
v_no emp.deptno%TYPE := 10;
--過期
PROCEDURE query_emp(v_deptno IN NUMBER DEFAULT v_no, v_avgsal OUT NUMBER, v_cnt OUT NUMBER);
END first_package;
--創建包體
CREATE OR REPLACE PACKAGE BODY first_package
IS
PROCEDURE query_emp(v_deptno IN NUMBER DEFAULT v_no, v_avgsal) OUT NUMBER, v_cnt OUT NUMBER)
IS
BEGIN
SELECT avg(sal), count(*) INTO v_avgsal, v_cnt FROM emp WHERE deptno = v_deptno;
EXCEPTION
WHEN NO_DATA_FOUND THEN
dbms_output.put_line('沒有此部門');
WHEN OTHERS THEN
dbms_output.put_line(SQLERRM);
END;
END first_package;
DECLARE
v_avgsal NUMBER;
v_cnt NUMBER;
BEGIN
first_package.query_emp(20, v_avgsal, v_cnt);
DBMS_OUTPUT.put_line('平均工資:' || v_avgsal);
DBMS_OUTPUT.put_line('總人數:' || v_cnt);
END;
CREATE [OR REPLACE] PACKAGE package_name
IS | AS
-- 定義公用常量、變量、游標、過程、函數等
END [package_name];
示例代碼:
CREATE OR REPLACE PACKAGE emp_package
IS
--添加員工信息的存儲過程
PROCEDURE add_emp_proc
(v_empno IN emp.empno%TYPE,
v_ename IN emp.ename%TYPE,
v_sal IN emp.sal%TYPE,
v_deptno IN emp.deptno%TYPE);
--刪除員工信息的存儲過程
PROCEDURE del_emp_proc
(v_empno IN emp.empno%TYPE);
END emp_package;
CREATE [OR REPLACE] PACKAGE BODY package_name
IS | AS
--定義私有常量、變量、游標、過程和函數等
--實現公用過程和函數
END [package_name];
示例代碼:
CREATE OR REPLACE PACKAGE BODY emp_package
IS
--添加員工信息的存儲過程
PROCEDURE add_emp_proc
(v_empno IN emp.empno%TYPE,
v_ename IN emp.ename%TYPE,
v_sal IN emp.sal%TYPE,
v_deptno IN emp.deptno%TYPE)
IS
e_2291 EXCEPTION;
PRAGMA EXCEPTION_INIT(e_2291, -2291);
BEGIN
INSERT INTO emp(empno, ename, sal, deptno) VALUES(v_empno, v_ename, v_sal, v_deptno);
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
RAISE_APPLICATION_ERROR(-20001, '員工號不能重復');
WHEN e_2291 THEN
RAISE_APPLICATION_ERROR(-20002, '部門號不存在');
END;
--刪除員工信息的存儲過程
PROCEDURE del_emp_proc
(v_empno IN emp.empno%TYPE)
IS
BEGIN
--根據員工號刪除指定的員工信息
DELETE FROM emp WHERE empno = v_empno;
--判斷是否刪除成功
IF SQL%NOTFOUND THEN
RAISE_APPLICATION_ERROR(-20009, '指定刪除的員工不存在');
ELSE
DBMS_OUTPUT.PUT_line('刪除成功');
END IF;
END;
END emp_package;
根據員工號查詢工資,如果工資小于等于3000,工資漲500。
--創建包規范
CREATE OR REPLACE PACKAGE emp_sal_pkg
IS
FUNCTION get_sal(eno NUMBER) RETURN NUMBER;
PROCEDURE upd_sal(eno NUMBER, salary NUMBER);
END emp_sal_pkg;
--包體
CREATE OR REPLACE PACKAGE BODY emp_sal_pkg
IS
FUNCTION get_sal(eno NUMBER) RETURN NUMBER
IS
v_sal emp.sal%TYPE := 0;
BEGIN
SELECT sal INTO v_sal FROM emp WHERE empno = eno;
RETURN v_sal;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20010,'此員工號不存在');
END;
PROCEDURE upd_sal(eno NUMBER, salary NUMBER)
IS
BEGIN
IF salary <=3000 THEN
UPDATE emp SET sal = sal + 500 WHERE empno = eno;
END IF;
END;
END emp_sal_pkg;
調用emp_package包下添加員工信息的存儲過程。(在本專題的第2節包的創建,已經在Oracle數據庫中創建emp_package包規范和包體)
DECLARE
v_empno emp.empno%TYPE := &empno;
v_ename emp.ename%TYPE := '&name';
v_sal emp.sal%TYPE := &salary;
v_deptno emp.deptno%TYPE := &deptno;
e_dup_val EXCEPTION;
e_no_dept EXCEPTION;
PRAGMA EXCEPTION_INIT(e_dup_val, -20001);
PRAGMA EXCEPTION_INIT(e_no_dept, -20002);
BEGIN
emp_package.add_emp_proc(v_empno, v_ename, v_sal, v_deptno);
COMMIT;
EXCEPTION
WHEN e_dup_val THEN
DBMS_OUTPUT.put_line(SQLERRM);
WHEN e_no_dept THEN
DBMS_OUTPUT.put_line(SQLERRM);
ROLLBACK;
END;
DECLARE
v_empno emp.empno%TYPE := &empno;
e_no_emp EXCEPTION;
PRAGMA EXCEPTION_INIT(e_no_emp, -20009);
BEGIN
emp_package.del_emp_proc(v_empno);
COMMIT;
EXCEPTION
WHEN e_no_emp THEN
DBMS_OUTPUT.put_line(SQLERRM);
ROLLBACK;
END;
DECLARE
v_empno emp.empno%TYPE := &empno;
v_salary emp.sal%TYPE;
e_no_emp EXCEPTION;
PRAGMA EXCEPTION_INIT(e_no_emp, -20010);
BEGIN
v_salary := emp_sal_pkg.get_sal(v_empno);
emp_sal_pkg.upd_sal(v_empno, v_salary);
COMMIT;
EXCEPTION
WHEN e_no_emp THEN
DBMS_OUTPUT.put_line(SQLERRM);
END;
示例1:在SQLPlus窗口中進行emp_sal_pkg包的調用,查詢員工的工資信息:
SQL> VAR v_empno NUMBER
SQL> EXEC :v_empno := &no
PL/SQL procedure successfully completed
v_empno
---------
1234
SQL> VAR v_salary NUMBER
SQL> EXEC :v_salary := emp_sal_pkg.get_sal(:v_empno)
begin :v_salary := emp_sal_pkg.get_sal(:v_empno); end;
ORA-20010: 此員工號不存在
ORA-06512: 在 "SCOTT.EMP_SAL_PKG", line 11
ORA-06512: 在 line 1
v_salary
---------
v_empno
---------
1234
示例2:在SQLPlus窗口中進行emp_sal_pkg包的調用,更新員工的工資信息:(變量v_empno已經在示例1中定義)
SQL> EXEC :v_empno := &no
PL/SQL procedure successfully completed
v_empno
---------
7369
SQL> EXEC :v_salary := emp_sal_pkg.get_sal(:v_empno)
PL/SQL procedure successfully completed
v_salary
---------
800
v_empno
---------
7369
SQL> EXEC emp_sal_pkg.upd_sal(:v_empno, :v_salary)
PL/SQL procedure successfully completed
v_empno
---------
7369
v_salary
---------
800
SQL> SELECT ename, sal from emp WHERE empno = 7369;
ENAME SAL
---------- ---------
G_EASON 1300.00
DROP PACKAGE [BODY] [user.] package_name;
示例:1、根據員工號或者員工姓名獲取員工信息;2、根據員工號或者員工姓名刪除員工的信息。
--創建包規范
CREATE OR REPLACE PACKAGE overload_pkg
IS
FUNCTION get_info(eno NUMBER) RETURN emp%ROWTYPE;
FUNCTION get_info(name VARCHAR) RETURN emp%ROWTYPE;
PROCEDURE del_emp(eno NUMBER);
PROCEDURE del_emp(name VARCHAR);
END;
--創建包體
CREATE OR REPLACE PACKAGE BODY overload_pkg
IS
FUNCTION get_info(eno NUMBER) RETURN emp%ROWTYPE
IS
emp_record emp%ROWTYPE;
BEGIN
SELECT * INTO emp_record FROM emp WHERE empno = eno;
RETURN emp_record;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20020, '不存在此員工');
END;
FUNCTION get_info (name VARCHAR) RETURN emp%ROWTYPE
IS
emp_record emp%ROWTYPE;
BEGIN
SELECT * INTO emp_record FROM emp WHERE ename= name;
RETURN emp_record;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20020, '不存在此員工');
END;
PROCEDURE del_emp(eno NUMBER)
IS
BEGIN
DELETE FROM emp WHERE empno = eno;
IF SQL%NOTFOUND THEN
RAISE_APPLICATION_ERROR(-20020, '不存在此員工');
END IF;
END;
PROCEDURE del_emp(name VARCHAR)
IS
BEGIN
DELETE FROM emp WHERE ename = name;
IF SQL%NOTFOUND THEN
RAISE_APPLICATION_ERROR(-20020, '不存在此員工');
END IF;
END;
END overload_pkg;
--根據員工號查詢員工信息
DECLARE
emp_record emp%rowtype;
e_no_emp EXCEPTION;
PRAGMA EXCEPTION_INIT(e_no_emp, -20020);
BEGIN
emp_record := overload_pkg.get_info(&no);
DBMS_OUTPUT.put_line('員工號:' || emp_record.empno || ',姓名:' || emp_record.ename || ',工資:' ||
emp_record.sal);
EXCEPTION
WHEN e_no_emp THEN
DBMS_OUTPUT.put_line(SQLERRM);
END;
--根據員工姓名查詢員工信息
DECLARE
emp_record emp%rowtype;
e_no_emp EXCEPTION;
PRAGMA EXCEPTION_INIT(e_no_emp, -20020);
BEGIN
emp_record := overload_pkg.get_info('&ename');
DBMS_OUTPUT.put_line('員工號:' || emp_record.empno || ',姓名:' || emp_record.ename || ',工資:' ||
emp_record.sal);
EXCEPTION
WHEN e_no_emp THEN
DBMS_OUTPUT.put_line(SQLERRM);
END;
--根據員工號刪除員工信息
DECLARE
e_no_emp EXCEPTION;
PRAGMA EXCEPTION_INIT(e_no_emp, -20020);
BEGIN
overload_pkg.del_emp(&no);
COMMIT;
EXCEPTION
WHEN e_no_emp THEN
DBMS_OUTPUT.put_line(SQLERRM);
ROLLBACK;
END;
--根據員工姓名刪除員工信息
DECLARE
e_no_emp EXCEPTION;
PRAGMA EXCEPTION_INIT(e_no_emp, -20020);
BEGIN
overload_pkg.del_emp('&ename');
COMMIT;
EXCEPTION
WHEN e_no_emp THEN
DBMS_OUTPUT.put_line(SQLERRM);
ROLLBACK;
END;
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。