日期:2014-05-17  浏览次数:21076 次

触发器可以由UPDATE某个字段触发吗?
SQL: update 表1 set 表1.字段1=XX,表1.字段2=XX;

我可以写个触发器,当update表1中的“字段1”时,我就更新另外一个表的字段3吗?
表1有很多字段,但是只有update字段1的时候才触发。

请问这个触发器该怎么写?


------解决方案--------------------
SQL code
--使用updating判断
create or replace trigger test_trig 
before update on 表1
begin
   if updating('字段1') then
      update 表2 set 字段3='<值>';
   end if;
   if updating('字段2') then
      dbms_output.put_line('更新字段2');
   end if;
end;

------解决方案--------------------
create or replace trigger tri
after update of 字段1 on 表1
for each row
declare
-- local variables here
begin
update 表2 set 字段3 = :new.字段1;
end tri;
------解决方案--------------------
create or replace trigger test_trig 
before update on t1
begin
if updating(‘a1‘) then
dbms_output.put_line(‘updating a1‘);
end if;
if updating(‘b1‘) then
dbms_output.put_line(‘updating b1‘);
end if;
end;

SQL> update t1 set a1=9 ;
updating a1

1 row updated.

SQL> update t1 set b1=9 ;
updating b1

1 row updated.
------解决方案--------------------
create or replace trigger tri
after update of 字段1 on 表1
for each row
declare
-- local variables here
begin
update 表2 set 字段3 = :new.字段1;
end tri;
------解决方案--------------------
after update of 字段1,字段2 on 表1
这个是可以的

update 表2 set 字段3 = :new.字段1,表3.字段3 = :new.字段2
这个是不行的,不能在一个语句处指定两张表的字段
------解决方案--------------------
改为 if :new.dreqdate is not null then 式下

------解决方案--------------------
呵呵 要么and 要么or