• 判断 SQLServer 触发器类型,支持多行


    直接上代码吧:

     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     begin
    13         set @opType = 'insert'
    14         declare cur cursor
    15         for select * from inserted
    16         open cur
    17         fetch next from cur
    18         --into @id
    19         while @@fetch_status = 0
    20         begin
    21             insert into [log](OpType,TableName) values (@opType,@tableName)
    22             fetch cur --into @id
    23         end
    24         close cur
    25         deallocate cur
    26     end
    27 --delete--------------------------------------------------------
    28 if exists(select * from deleted) and not exists (select * from inserted)
    29     begin
    30         set @opType = 'delete'
    31         declare cur cursor
    32         for select * from deleted
    33         open cur
    34         fetch next from cur
    35         --into @id
    36         while @@fetch_status = 0
    37         begin
    38             insert into [log](OpType,TableName) values (@opType,@tableName)
    39             fetch cur --into @id
    40         end
    41         close cur
    42         deallocate cur
    43     end
    44 --update--------------------------------------------------------
    45 if exists(select * from deleted) and exists (select * from inserted)
    46     begin
    47         set @opType = 'update'
    48         declare cur cursor
    49         for select * from deleted
    50         open cur
    51         fetch next from cur
    52         --into @id
    53         while @@fetch_status = 0
    54         begin
    55             insert into [log](OpType,TableName) values (@opType,@tableName)
    56             fetch cur --into @id
    57         end
    58         close cur
    59         deallocate cur
    60     end
    61 end

    作者:shungdawei
    出处:http://www.cnblogs.com/shungdawei
    本页版权归作者和博客园所有,欢迎转载,但未经作者同意必须保留此段声明, 且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。

  • 相关阅读:
    用魔数防范文件上传攻击
    nginx http跳转到https
    tengine安装
    版本标记说明
    nginx基于域名的虚拟主机 反向代理配置实例
    非ROOT用户启动Tomcat
    使用druid连接池的超时回收机制排查连接泄露问题
    Jenkins入门系列之
    centos7 关闭SELINUX 防火墙
    mac安装IE浏览器
  • 原文地址:https://www.cnblogs.com/shungdawei/p/2671943.html
Copyright © 2020-2023  润新知