日期:2014-05-17 浏览次数:20457 次
create table tb(代号 nvarchar(20),父代号 nvarchar(20),名称 nvarchar(20),数量 int)
insert into tb select '001','0','电焊',5
insert into tb select '001001','001','车工',2
insert into tb select '001002','001','机车',3
insert into tb select '002','0','汽车',5
insert into tb select '002001','002','坦克',5
insert into tb select '003','0','飞机',6
go
create trigger whereinsert
on tb
after insert
as
begin
update tb set 数量=b.数量+a.数量 from tb b inner join inserted a on b.代号=a.父代号
end
go
create trigger whereupdate
on tb
after update
as
begin
update tb set 数量=b.数量+a.数量-c.数量 from tb b inner join inserted a on b.代号=a.父代号 inner join deleted c on b.代号=c.父代号
end
go
create trigger wheredelete
on tb
after delete
as
begin
update tb set 数量=b.数量-c.数量 from tb b inner join deleted c on b.代号=c.父代号
end
go
insert into tb select '003001','003','喷气式客机',10
select * from tb
update tb set 数量=3 where 代号='003001'
select * from tb
delete from tb where 代号='003001'
select * from tb
go
drop table tb
/*
代号 父代号 名称 数量
-------------------- -------------------- -------------------- -----------
001 0 &nbs