--建表 create table test(a varchar2(20),b varchar2(20));--触发器(使用中间变量交换就行了) CREATE OR REPLACE TRIGGER tr_test BEFORE INSERT ON test FOR EACH ROW DECLARE temp VARCHAR2(20); BEGIN temp := :new.a; :new.a := :new.b; :new.b := temp; END; /
将更改的数据信息调换,不就是借助一个中间变量嘛! c++里面经常会用到一个自定义的swap()函数;create table t( c1 varchar2(10 char), a number, b number) / create or replace trigger tri_insert before insert on t --使用before insert触发器的原因,数据在插入之前就应该更改, --而不使用after insert触发器,数据insert之后就不能任意的移动位置, --除非使用update语句来更改表数据 for each row declare temp number; begin temp := :new.a; :new.a := :new.b; :new.b := temp; end tri_insert; insert into t select 'aaa',56,65 from dual union all select 'bbb',78,87 from dual union all select 'ccc',69,96 from dual / SQL> select * from t;
C1 A B -------------------- ---------- ---------- aaa 65 56 bbb 87 78 ccc 96 69
--如果使用after insert触发器,你将会得到下面的错误提示: ORA-04084: cannot change NEW values for this trigger type
create table test(a varchar2(20),b varchar2(20));--触发器(使用中间变量交换就行了)
CREATE OR REPLACE TRIGGER tr_test
BEFORE INSERT ON test
FOR EACH ROW
DECLARE
temp VARCHAR2(20);
BEGIN
temp := :new.a;
:new.a := :new.b;
:new.b := temp;
END;
/
c++里面经常会用到一个自定义的swap()函数;create table t(
c1 varchar2(10 char),
a number,
b number)
/
create or replace trigger tri_insert
before insert on t
--使用before insert触发器的原因,数据在插入之前就应该更改,
--而不使用after insert触发器,数据insert之后就不能任意的移动位置,
--除非使用update语句来更改表数据
for each row
declare
temp number;
begin
temp := :new.a;
:new.a := :new.b;
:new.b := temp;
end tri_insert;
insert into t
select 'aaa',56,65 from dual union all
select 'bbb',78,87 from dual union all
select 'ccc',69,96 from dual
/
SQL> select * from t;
C1 A B
-------------------- ---------- ----------
aaa 65 56
bbb 87 78
ccc 96 69
--如果使用after insert触发器,你将会得到下面的错误提示:
ORA-04084: cannot change NEW values for this trigger type