楼主啥数据库?
Oracle 和 SQL Server 处理机制不一样的。
Oracle
通过
OF 字段 ON 表
实现只针对特定列的触发。
FOR EACH ROW 表示这个触发器为 行级触发器。
测试表:
SQL> CREATE TABLE test_trigger_table (
2 id INT,
3 name VARCHAR(10),
4 val INT
5 );
Table created.
SQL> CREATE OR REPLACE TRIGGER BeforeUpdateTest
2 BEFORE INSERT OR UPDATE OF val ON test_trigger_table
3 FOR EACH ROW
4 BEGIN
5 dbms_output.put_line('BEFORE UPDATE val ON test_trigger_table ');
6 dbms_output.put_line('Old val = ' || :old.val);
7 dbms_output.put_line('New val = ' || :new.val);
8 END;
9 /
Trigger created.
SQL>
SQL> INSERT INTO test_trigger_table(id, name, val) VALUES (1, 'ABC', 1);
BEFORE UPDATE val ON test_trigger_table
Old val =
New val = 1
1 row created.
SQL>
SQL> UPDATE test_trigger_table SET name = 'XYZ' WHERE id=1;
1 row updated.
SQL>
SQL> UPDATE test_trigger_table SET val = 20 WHERE id=1;
BEFORE UPDATE val ON test_trigger_table
Old val = 1
New val = 20
1 row updated.
-----------------------------
假如有一个日志记录表 叫 test_trigger_log
有6个字段。
分别存储 更新前 与 更新后:
所有字段更新都触发
CREATE OR REPLACE TRIGGER BeforeUpdateTest
BEFORE UPDATE ON test_trigger_table
FOR EACH ROW
BEGIN
INSERT INTO test_trigger_log (oldid, oldname, oldval, newid, newname, newval)
VALUES(:old.id, :old.name, :old.val, :new.id, :new.name, :new.val);
END;
create or replace trigger a5_buydate
before update on prline for each row
begin
if :old.prlineid <> :new.prlineid then
:new.buyer_date:= sysdate;
end if;
end;