慕田峪7331174
Oracle 这样做的方式是在视图上使用而不是触发器。这是一个基于 Scott 模式的示例。首先,我将创建一个视图作为两个表的连接,EMP并且DEPT(这就是一起更新两个表进入游戏的地方):SQL> create or replace view v_ed as 2 select d.deptno, e.empno, d.dname, e.ename, e.sal 3 from emp e join dept d on e.deptno = d.deptno;View created.现在,一个代替触发器。我正在处理INSERT和UPDATE;你也可以添加DELETE。这意味着 - 当您插入或更新视图时,基础表将成为这些命令的最终目标。SQL> create or replace trigger trg_io_ed 2 instead of insert or update on v_ed 3 for each row 4 begin 5 if inserting then 6 insert into emp (deptno, empno, ename, sal) 7 values (:new.deptno, :new.empno, :new.ename, :new.sal); 8 insert into dept (deptno, dname) 9 values (:new.deptno, :new.dname); 10 elsif updating then 11 update emp set 12 deptno = :new.deptno, 13 ename = :new.ename, 14 sal = :new.sal 15 where empno = :new.empno; 16 update dept set 17 dname = :new.dname 18 where deptno = :new.deptno; 19 end if; 20 end; 21 /Trigger created.一些测试insert:SQL> insert into v_ed (deptno, empno, dname, ename, sal) 2 values (99, 100, 'test dept', 'Littlefoot', 1000);1 row created.SQL> select * From dept; DEPTNO DNAME LOC---------- -------------- ------------- 10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON 99 test deptSQL> select * From emp; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO---------- ---------- --------- ---------- -------- ---------- ---------- ---------- 7369 SMITH CLERK 7902 17.12.80 800 20 7499 ALLEN SALESMAN 7698 20.02.81 1600 300 30 7521 WARD 7698 22.02.81 1250 500 7566 JONES MANAGER 7839 02.04.81 2975 20 7654 MARTIN SALESMAN 7698 28.09.81 1250 1400 30 7698 BLAKE MANAGER 7839 01.05.81 2850 7782 CLARK MANAGER 7839 09.06.81 2450 10 7788 SCOTT ANALYST 7566 09.12.82 3000 20 7839 KING PRESIDENT 17.11.81 5000 10 7844 TURNER SALESMAN 7698 08.09.81 1500 0 30 7876 ADAMS CLERK 7788 12.01.83 1100 20 7900 JAMES CLERK 7698 03.12.81 950 30 7902 FORD ANALYST 7566 03.12.81 3000 20 7934 MILLER CLERK 7782 23.01.82 1300 10 100 Littlefoot 1000 9915 rows selected.SQL>Update:SQL> update v_ed set ename = 'Bigfoot' where empno = 100;1 row updated.SQL> select * From emp; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO---------- ---------- --------- ---------- -------- ---------- ---------- ---------- 7369 SMITH CLERK 7902 17.12.80 800 20 7499 ALLEN SALESMAN 7698 20.02.81 1600 300 30 7521 WARD 7698 22.02.81 1250 500 7566 JONES MANAGER 7839 02.04.81 2975 20 7654 MARTIN SALESMAN 7698 28.09.81 1250 1400 30 7698 BLAKE MANAGER 7839 01.05.81 2850 7782 CLARK MANAGER 7839 09.06.81 2450 10 7788 SCOTT ANALYST 7566 09.12.82 3000 20 7839 KING PRESIDENT 17.11.81 5000 10 7844 TURNER SALESMAN 7698 08.09.81 1500 0 30 7876 ADAMS CLERK 7788 12.01.83 1100 20 7900 JAMES CLERK 7698 03.12.81 950 30 7902 FORD ANALYST 7566 03.12.81 3000 20 7934 MILLER CLERK 7782 23.01.82 1300 10 100 Bigfoot 1000 9915 rows selected.SQL>看看有没有帮助。