触发器使用的inserted临时表和deleted临时表
1不论后触发或替代触发,每个触发器被激活时,系统都自动为它们创建两个临时表:inserted和deleted表。
2两个表的结构与激活触发器的原数据表结构相同。
3用INSERT语句插入记录激活触发器时,系统在原表插入记录的同时,也自动把记录插入到inserted临时表。
4用SELECT语句删除记录激活触发器时,系统在原表删除记录的同时,会把删除的记录添加到deleted临时表。
5用UPDATE语句修改数据激活触发器时,系统先在原表删除原有记录,删除的记录被添加到deleted临时表,然后再插入新记录,并同时插入到inserted临时表。
6用户可以用SELECT语句查询这两个临时表,但不允许进行修改。
7触发器一旦执行完成,这两个表将被自动删除
如果被操作的数据是多值的,可用IN判断是否被包含在其中:
被操作数据 IN (SELECT 被操作字段 FROM 临时表)
如果被操作的数据是单值的,可用以下语句获得:
SELECT @变量=被操作字段 FROM 临时表
删除触发器
算每一行合差的触发器
Code
alter trigger trigger_data_getsum
on ttools_data
after insert,update,delete
as
DECLARE FIDcurcursor CURSOR
for
select d_fold_id from inserted group by d_fold_id
open FIDcurcursor
declare @d_fold_id int
FETCH NEXT FROM FIDcurcursor into @d_fold_id
WHILE @@FETCH_STATUS = 0
begin
DECLARE MYcursor CURSOR
FOR
select d_id, d_value,d_type,d_sum from ttools_data where d_fold_id= @d_fold_id order by d_date,d_id
open MYcursor
declare @d_id int, @d_value float, @d_type int, @d_sum float, @sum float
set @sum = 0
FETCH NEXT FROM MYcursor into @d_id,@d_value,@d_type,@d_sum
WHILE @@FETCH_STATUS = 0
begin
if @d_type = 1
set @sum = @sum + @d_value
else if @d_type = 2
set @sum = @sum - @d_value
update ttools_data set d_sum = @sum where d_fold_id= @d_fold_id and d_id = @d_id
FETCH NEXT FROM MYcursor into @d_id,@d_value,@d_type,@d_sum
end
CLOSE MYcursor
DEALLOCATE MYcursor
end
CLOSE FIDcurcursor
DEALLOCATE FIDcurcursor
INSERT INTO ttools_data([d_fold_id],[d_user_id],[d_value],[d_type],
[d_catalog_id],[d_date],[d_add_time],[d_info],d_sum)VALUES(4,1,3.4,2,8,'2009-1-1',getdate(),'sss',0)
Code
--A表主表 B表 级联的表
create trigger dropempid
on myemp
after delete
as
delete from B where [id] in (select empid from deleted)
--in (select empid from deleted) deleted表中的内容就是 A表删除的内容
delete from myemp where empid = 34
insert,update触发器
Code
create trigger a
on myemp
after insert,update
as
declare @fname nvarchar(50),@lname nvarchar(50),@aname nvarchar(50),@empid int
select @empid=empid, @fname=fname,@lname=lname from inserted
set @aname = @fname + @lname
update myemp set aname = @aname where empid=@empid
--把值先从inserted中取出来
update myemp set fname='1111111',lname='2222222222' where empid =30
insert into myemp (fname,lname) values ('wwwwwwwwww','hhhhhhhhhhhh')
Code
alter trigger t_trigger
on t
after insert
as
declare @rc int
set @rc = @@rowcount
if @rc = 0 return
declare @tname nvarchar(50),@tid int
if @rc = 1
begin
select @tid = tid,@tname = tname from inserted
print 'id= '+ cast(@tid as nvarchar(10)) + ','+'name=' + @tname
end
else
begin
select * into #t from inserted
create unique clustered index idx_id on #t(tid)
select @tid = tid,@tname = tname from (select top 1 tid,tname from #t order by tid) as d --赋值
while @@rowcount >0
begin
print 'id= '+ cast(@tid as nvarchar(10)) + ','+'name=' + @tname
select @tid = tid,@tname = tname from (select top 1 tid,tname from #t where tid>@tid order by tid) as d
end
end
delete from t
insert into t select 1,'a'
union all
select 2,'b'
union all
select 3,'c'
union all
select 4,'d'
Code
CREATE TRIGGER AddMessage
ON Topic
FOR INSERT
AS
INSERT INTO Message (
[ModuleID],
[TopicID],
[MessageUserID],
[MessageCreateTime],
[MessageIP]
)
select [ModuleID],
[TopicID],
[CreatedByUser],
[CreatedDate],
[CreatedIP]
from inserted
INSERT INTO Message (
[ModuleID],
[TopicID],
[MessageUserID],
[MessageCreateTime],
[MessageIP]
) values (1,1,1,getdate(),1)
发表于
2008-04-23 18:04
小昊
阅读( 1092)
评论()
编辑
收藏
举报
|
|