开始
set serveroutput on; DEFINE p_deptno = 10 DECLARE e_emps_remaining EXCEPTION; PRAGMA EXCEPTION_INIT (e_emps_remaining, -2292); BEGIN DELETE FROM dept WHERE deptno = & p_deptno; COMMIT; EXCEPTION WHEN e_emps_remaining THEN DBMS_OUTPUT.PUT_LINE('Cannot remove dept' || TO_CHAR(&p_deptno) || '. Employees exist. '); END;
运行结果:
anonymous block completed
Cannot remove dept10. Employees exist.
至于为何,那是因为在 emp 表上有 constraint 存在。
CONSTRAINT "FK_DEPTNO" FOREIGN KEY ("DEPTNO") REFERENCES "SCOTT"."DEPT" ("DEPTNO") ENABLE
结束
本文转自健哥的数据花园博客园博客,原文链接:http://www.cnblogs.com/gaojian/archive/2012/11/23/2784285.html,如需转载请自行联系原作者