直接上代码吧:
1 ALTER TRIGGER [dbo].[tgr_forOpType] 2 ON [dbo].[OpTypeLog] 3 FOR INSERT, UPDATE, DELETE 4 AS 5 begin 6 declare @id int 7 declare @tableName varchar(50) 8 set @tableName = 'TableName' 9 declare @opType varchar(50)10 --insert--------------------------------------------------------11 if exists(select * from inserted) and not exists (select * from deleted)12 begin13 set @opType = 'insert'14 declare cur cursor15 for select * from inserted16 open cur17 fetch next from cur18 --into @id19 while @@fetch_status = 020 begin21 insert into [log](OpType,TableName) values (@opType,@tableName)22 fetch cur --into @id23 end24 close cur25 deallocate cur26 end27 --delete--------------------------------------------------------28 if exists(select * from deleted) and not exists (select * from inserted)29 begin30 set @opType = 'delete'31 declare cur cursor32 for select * from deleted33 open cur34 fetch next from cur35 --into @id36 while @@fetch_status = 037 begin38 insert into [log](OpType,TableName) values (@opType,@tableName)39 fetch cur --into @id40 end41 close cur42 deallocate cur43 end44 --update--------------------------------------------------------45 if exists(select * from deleted) and exists (select * from inserted)46 begin47 set @opType = 'update'48 declare cur cursor49 for select * from deleted50 open cur51 fetch next from cur52 --into @id53 while @@fetch_status = 054 begin55 insert into [log](OpType,TableName) values (@opType,@tableName)56 fetch cur --into @id57 end58 close cur59 deallocate cur60 end61 end