日期:2014-05-18 浏览次数:20359 次
----状态1:如果录入下面数据:A表:rmk='是' id cp price oldprice 001 A 10 20 001 B 15 10
create table A(id int,rmk nvarchar(10)) create table A_dtl(id int,cp varchar(10),price float,oldprice float) Create trigger tr_xxx on A_dtl for insert,update as --insert IF EXISTS(SELECT 1 FROM inserted) AND NOT EXISTS(SELECT 1 FROM deleted) begin insert into A select id,case when price>oldprice then '否' else '是' end AS rmk from inserted end --update IF EXISTS(SELECT 1 FROM inserted) AND EXISTS(SELECT 1 FROM deleted) begin update a set a.rmk=(case when price>oldprice then '否' else '是' end) from A a,inserted b where a.id=b.id end --测试 insert A_dtl select 2,'cp',300,200 select * from A update A_dtl set price=100 where id=2 select * from A