zoukankan      html  css  js  c++  java
  • TRIGGER的使用(修改SP自动触发)

     1 CREATE TRIGGER [trg_save_change_SP] 
     2 ON DATABASE 
     3 FOR CREATE_PROCEDURE, ALTER_PROCEDURE,DROP_PROCEDURE
     4 AS
     5     DECLARE @data XML
     6     DECLARE @InstanceName nvarchar(200),
     7         @DBName    nvarchar(100) ,
     8         @ObjectID int, 
     9         @Version    int ,         
    10         @DBUser    nvarchar(100),
    11         @InDateTime datetime, 
    12         @HostName nvarchar(200),
    13         @LoginName nvarchar(100),
    14         @EventName nvarchar(100),    
    15         @ObjectName nvarchar(200)  ,            
    16         @TSQL nvarchar(max),
    17         @ObjectType char(2),
    18         @SeqNo int            
    19             
    20     SET @data = EVENTDATA()
    21 
    22     SELECT  
    23         @InstanceName = @@SERVERNAME   ,
    24         @DBName = DB_NAME(),
    25         @HostName = hostname,  
    26         @DBUser = CONVERT(nvarchar(100), CURRENT_USER), 
    27         @LoginName = @data.value('(/EVENT_INSTANCE/LoginName)[1]', 'nvarchar(100)'), 
    28         @EventName = @data.value('(/EVENT_INSTANCE/EventType)[1]', 'nvarchar(100)'), 
    29         @ObjectName= @data.value('(/EVENT_INSTANCE/ObjectName)[1]', 'nvarchar(400)'),
    30         @TSQL = @data.value('(/EVENT_INSTANCE/TSQLCommand)[1]', 'nvarchar(max)') 
    31     FROM master..sysprocesses WHERE spid = @@spid    
    32     
    33     SELECT     @Version = ISNULL(MAX(Version),0) + 1 FROM admin.dbo.ObjectLog WHERE ObjectName = @ObjectName AND DBName = @DBName
    34 
    35     SELECT @ObjectType = type FROM sys.objects WHERE name = @ObjectName
    36     
    37     SELECT @SeqNo = ISNULL(MAX(SeqNo),0)+1 FROM admin.dbo.ObjectLog
    38     
    39     INSERT admin.dbo.ObjectLog(
    40         [SeqNo]
    41       , [DBName]
    42       , [ObjectID]
    43       , [ObjectName]
    44       , [Version]
    45       , [EventName]
    46       , [DBUser]
    47       , [HostName]
    48       , [LoginName]
    49       , [InDateTime]
    50       , [TSQL]
    51       , [CheckInChk]
    52       , [InstanceName]
    53       , [ObjectType]
    54 )
    55 VALUES(
    56           @SeqNo
    57         , @DBName
    58         , Object_ID(@ObjectName)
    59         , @ObjectName
    60         , @Version
    61         , @EventName
    62         , @DBUser
    63         , LTRIM(RTRIM(@HostName))
    64         , @LoginName
    65         , GETDATE()
    66         , @TSQL
    67         , '0'
    68         , @InstanceName
    69         , @ObjectType)
  • 相关阅读:
    leetcode 279. Perfect Squares
    leetcode 546. Remove Boxes
    leetcode 312. Burst Balloons
    leetcode 160. Intersection of Two Linked Lists
    leetcode 55. Jump Game
    剑指offer 滑动窗口的最大值
    剑指offer 剪绳子
    剑指offer 字符流中第一个不重复的字符
    leetcode 673. Number of Longest Increasing Subsequence
    leetcode 75. Sort Colors (荷兰三色旗问题)
  • 原文地址:https://www.cnblogs.com/mybky/p/3798639.html
Copyright © 2011-2022 走看看