table1 有3个字段(序号,字母 varchar2,数字 varchar2)序号   字母    数字
001     A        2
002     E        5如果更新的值是 '不变',那么值不变例 update table1 set 字母='不变',数字='100' where 序号=001
   update table1 set 字母='不变',数字='不变' where 序号=002更新后:序号   字母    数字
        001     A       100
        002     E        5请问这个触发器该怎么写

解决方案 »

  1.   

    create or replace trigger tgname
    before update on table1
    for each row
    when(new.字母='不变' or new.数字='不变')
    begin
    if :new.字母='不变' then
      :new.字母:=:old.字母;
    end if;
    if :new.数字='不变' then
      :new.数字:=:old.数字;
    end if;
    end;不过一般不要这样用,既然不需要更新为什么要更新成'不变'...
    update table1 set 数字='100' where 序号=001 不就好了
      

  2.   


    SQL> drop table table1;表已丢弃。SQL> create table table1(序号 varchar2(100),字母 varchar2(100),数字 varchar2(100));表已创建。SQL> insert into table1 values('001','A','2');已创建 1 行。SQL> insert into table1 values('002','E','5');已创建 1 行。SQL> select * from table1;序号       字母       数字
    ---------- ---------- ----------
    001        A          2
    002        E          5SQL> create or replace trigger t_update_test
      2  before update on table1
      3  for each row
      4  when(new.字母='不变' or new.数字='不变')
      5  begin
      6  if :new.字母 = '不变' then
      7        :new.字母 := :old.字母;
      8  end if;
      9  if :new.数字 = '不变' then
     10        :new.数字 := :old.数字;
     11  end if;
     12  end;
     13  /触发器已创建SQL>
    SQL> update table1 set 字母='不变',数字='100' where 序号='001';已更新 1 行。SQL> update table1 set 字母='不变',数字='不变' where 序号='002';已更新 1 行。SQL> select * from table1;序号       字母       数字
    ---------- ---------- ----------
    001        A          100
    002        E          5SQL>
      

  3.   


    --更新后抛异常信息的写法。
    create or replace trigger trigger_table1
    before update on table1
    for each row
    when(new.字母='不变' or new.数字='不变')
    begin
        raise_application_error('-20001','字段[字母]或者[数字]不允许更新成[不变]');
    end;
    /--更新后把原值更新回去的写法。
    create or replace trigger trigger_table1
    before update on table1
    for each row
    when(new.字母='不变' or new.数字='不变')
    begin
        :new.字母=:old.字母;
        :new.数字=:old.数字;
    end;
    /