我需要编写一个过程并更新触发器。在表上完成任何更新后,触发器应调用该过程。过程应该在另一个表中更新更改。在另一个表的旧值中,应该有更新的值。
答案 0 :(得分:2)
您描述的内容听起来像是普通日志记录;您实际上并不需要过程,触发器就可以完成。这是一个示例:
SQL> create table emp_log (empno number, sal_old number, sal_new number);
Table created.
SQL> create or replace trigger trg_bu_emp
2 before update of sal on emp
3 for each row
4 begin
5 insert into emp_log (empno, sal_old, sal_new)
6 values
7 (:new.empno, :old.sal, :new.sal);
8 end;
9 /
Trigger created.
SQL> select empno, ename, sal from emp where ename = 'KING';
EMPNO ENAME SAL
---------- ---------- ----------
7839 KING 5000
SQL> update emp set sal = 7000 where ename = 'KING';
1 row updated.
SQL> select * from emp_log;
EMPNO SAL_OLD SAL_NEW
---------- ---------- ----------
7839 5000 7000
SQL>
[阅读评论后,编辑]
作业,是吗?所以-创建一个过程:
SQL> rollback;
Rollback complete.
SQL> create or replace procedure p_emp_sal_log
2 (par_empno in emp.empno%type, par_sal_old in emp.sal%type,
3 par_sal_new in emp.sal%type)
4 is
5 begin
6 insert into emp_log (empno, sal_old, sal_new)
7 values
8 (par_empno, par_sal_old, par_sal_new);
9 end;
10 /
Procedure created.
SQL> create or replace trigger trg_bu_emp
2 before update of sal on emp
3 for each row
4 begin
5 p_emp_sal_log(:new.empno, :old.sal, :new.sal);
6 end;
7 /
Trigger created.
SQL> update emp set sal = 2000 where ename = 'KING';
1 row updated.
SQL> select * from emp_log;
EMPNO SAL_OLD SAL_NEW
---------- ---------- ----------
7839 5000 2000
SQL>