select * from info
select * from work
select * from family
delete from INFo where code='p002'
drop trigger tr_info_add --删除触发器
go
alter trigger tr_info_delete on info
instead of delete
as
declare @code varchar(50)
select @code=code from deleted
delete from work where infocode=@code
delete from family where infocode=@code
delete from info where code=@code
go
go
--案例一:
alter trigger tr_info_add on info
for insert,delete
as
print '改变了一条数据'
go
--案例二:建一个触发器,在触发器中用select显示inserted表和deleted表中的数据。
--案例三:水果表改变numbers列时,向orderdetails表中添加数据。
--案例四:instead of触发器,删除info表的数据时,先删除work和family的数据,再删info相应的数据。
insert into INFO values('p006','李四','1','n002','1999-3-2')
go
alter trigger tr_fruit_update on fruit
for update
as
--当水果表修改,就向fruitdetails中添加数据
--水果代号--fruitcode;number的减少量--count
--把修改的对应数据取出来。水果代号,数量差
declare @code varchar(50)
declare @num1 int
declare @num2 int
declare @num int
select @code=ids ,@num1=numbers from deleted
select @num2=numbers from inserted
set @num=@num1-@num2
--向orderdetails表中执行插入操作
insert into orderdetails (fruitcode,[count]) values(@code,@num)
--在触发器中有两个临时表。表名是:inserted,deleted,这两个临时表中最多只有一条数据。
--print 'deleted表的数据:'
--select * from deleted
--print 'inserted表中的数据'
--select * from inserted
go
update Fruit set Numbers=95 where Ids='k003'