日期:2014-05-17 浏览次数:20967 次
create table testa ( sname varchar2(30), stime varchar2(20) ); create table testb as select * from testa; create or replace trigger tri_testb after insert on testa for each row declare -- local variables here begin if inserting then merge into testb a using (select :new.sname as sname, :new.stime as stime from dual) b on (a.sname = b.sname) when matched then update set a.stime = b.stime when not matched then insert (a.sname, a.stime) values (b.sname, b.stime); end if; end tri_testb; / select * from testb; insert into testa values('张三', '20110912'); insert into testa values('张三', '20110913'); insert into testa values('张三', '20110914'); insert into testa values('李四', '20110912'); insert into testa values('张三', '20110913'); commit; select * from testb; insert into testa values('张三', '20110915'); commit; select * from testb;
------解决方案--------------------
建立一个针对A表insert的触发器,如果表为a、b,字段为name,time
CREATE OR REPLACE TRIGGER TR_AUTO
after INSERT ON 表A
FOR EACH ROW
DECLARE
NEXTID NUMBER;
BEGIN
SELECT SEQ_ADV_IDX.NEXTVAL INTO NEXTID FROM DUAL;
update b set b.time= :NEW.time where b.name=:NEW.name
END;
------解决方案--------------------
楼上都已经些出来了,我就不再写了 这个其实就是触发器的基本功能啊: A表插入后 (after insert)触发触发器, 触发器的功能有两步:1,查询B表(查看B表中是否已经有该数据了);2,根据查询结果作更新或插入操作(1的返回结果为true,则更新B表;否则插入B表) ps:A表中新插入的数据可以用(:NEW.xxxx)取得