日期:2014-05-18 浏览次数:20793 次
create trigger tg_tb on Gbaozhuangjihua after insert,update as begin update a set a.包装ID = b.id from Gbaozhuangjihua a join gbaozhuangxingxi b on a.包装代码 = b.包装代码 where b.id <> a.包装ID end
------解决方案--------------------
create table gbaozhuangxingxi (包装ID int, 包装代码 varchar(20), 数量 int, 单位 varchar(10)) insert into gbaozhuangxingxi select 1, 'ZX-10-10-1-CPT', 20, '件/箱' union all select 2, 'ZX-10-10-2-CPT', 30, '件/箱' union all select 3, 'ZX-10-10-3-CPT', 40, '件/箱' union all select 4, 'ZX-10-10-4-CPT', 50, '件/箱' create table Gbaozhuangjihua (包装计划ID int, 包装代码 varchar(20), 计划数 int, 包装ID int) insert into Gbaozhuangjihua select 1, 'ZX-10-10-1-CPT', 5, 1 union all select 2, 'ZX-10-10-2-CPT', 6, 2 union all select 3, 'ZX-10-10-3-CPT', 7, 3 union all select 4, 'ZX-10-10-4-CPT', 8, 4 create trigger tr_Gbaozhuangjihua on Gbaozhuangjihua for update as begin if update(包装代码) begin if exists(select 1 from gbaozhuangxingxi a inner join inserted b on a.包装代码=b.包装代码) update a set a.包装ID= (select a.包装ID from gbaozhuangxingxi a inner join inserted b on a.包装代码=b.包装代码) from Gbaozhuangjihua a inner join inserted b on a.包装计划ID=b.包装计划ID else begin print '包装代码表中不存在相应代码信息.' rollback tran end end end -- 测试更新为 ZX-10-10-2-CPT update Gbaozhuangjihua set 包装代码='ZX-10-10-2-CPT' where 包装计划ID=3 select * from Gbaozhuangjihua 包装计划ID 包装代码 计划数 包装ID ----------- -------------------- ----------- ----------- 1 ZX-10-10-1-CPT 5 1 2 ZX-10-10-2-CPT 6 2 3 ZX-10-10-2-CPT 7 2 4 ZX-10-10-4-CPT 8 4 -- 测试更新为不存在的包装代码 update Gbaozhuangjihua set 包装代码='abc' where 包装计划ID=3 select * from Gbaozhuangjihua 包装计划ID 包装代码 计划数 包装ID ----------- -------------------- ----------- ----------- 1 ZX-10-10-1-CPT 5 1 2 ZX-10-10-2-CPT 6 2 3 ZX-10-10-2-CPT 7 2 4 ZX-10-10-4-CPT 8 4
------解决方案--------------------