创建审计表

时间:2016-03-13 12:00:20

标签: sql-server tsql triggers audit

从下面的代码

  1. 我需要XML格式的OldValueNewValue
  2. @PKSelect字符串值中,应该像'孟买',带引号
  3. Concat @PKFieldSelect@PKValueSelect@PKSelect
  4. 请建议我做上面的问题

    create TRIGGER auditrg ON [tablename] FOR INSERT, UPDATE, DELETE
    AS
    
    DECLARE 
           @TableName VARCHAR(128) ,
           @PKCols VARCHAR(1000) ,
           @sql VARCHAR(2000), 
           @UpdateDate VARCHAR(21) ,
           @UserName VARCHAR(128) ,
           @Type CHAR(1) ,
           @PKSelect nVARCHAR(max),
           @OldValue nVARCHAR(max),
           @NewValue nVARCHAR(max),
           @PKFieldSelect nVARCHAR(max),
           @PKValueSelect nVARCHAR(max)
    begin
    --You will need to change @TableName to match the table to be audited
    SELECT @TableName = 'tablename'
    SELECT @UserName = SYSTEM_USER ,
           @UpdateDate = CONVERT(VARCHAR(8), GETDATE(), 112)+ ' ' + CONVERT(VARCHAR(12), GETDATE(), 114)
    
    -- Action
          IF EXISTS (SELECT * FROM inserted)
           IF EXISTS (SELECT * FROM deleted)
                   SELECT @Type = 'U'
            ELSE
                   SELECT @Type = 'I'
            ELSE
                   SELECT @Type = 'D'              
    
        SET @OldValue = (SELECT * FROM DELETED  FOR XML AUTO,BINARY BASE64)  
        SET @NewValue = (SELECT * FROM INSERTED  FOR XML AUTO,BINARY BASE64)
    
    -- get list of columns
    
    SELECT * INTO #ins FROM inserted 
    SELECT * INTO #del FROM deleted
    
    
    SET QUOTED_IDENTIFIER ON
    -- Get primary key columns for full outer join
    SELECT @PKCols = COALESCE(@PKCols + ' and', ' on') 
                   + ' i.' + c.COLUMN_NAME + ' = d.' + c.COLUMN_NAME
           FROM    INFORMATION_SCHEMA.TABLE_CONSTRAINTS pk ,
                  INFORMATION_SCHEMA.KEY_COLUMN_USAGE c
           WHERE   pk.TABLE_NAME = @TableName
           AND     CONSTRAINT_TYPE = 'PRIMARY KEY'
           AND     c.TABLE_NAME = pk.TABLE_NAME
           AND     c.CONSTRAINT_NAME = pk.CONSTRAINT_NAME 
    
    -- Get primary key fields select for insert       
    SELECT @PKFieldSelect = COALESCE(@PKFieldSelect+'+','') 
           + '''' + COLUMN_NAME +''''
           FROM    INFORMATION_SCHEMA.TABLE_CONSTRAINTS pk ,
                   INFORMATION_SCHEMA.KEY_COLUMN_USAGE c
           WHERE   pk.TABLE_NAME = @TableName
           AND     CONSTRAINT_TYPE = 'PRIMARY KEY'
           AND     c.TABLE_NAME = pk.TABLE_NAME
           AND     c.CONSTRAINT_NAME = pk.CONSTRAINT_NAME  
    
    select @PKValueSelect = coalesce(@PKValueSelect+'+','') + 'convert(varchar(100), coalesce(i.' + COLUMN_NAME + ',d.' + COLUMN_NAME + '))'
    from INFORMATION_SCHEMA.TABLE_CONSTRAINTS pk ,
    INFORMATION_SCHEMA.KEY_COLUMN_USAGE c
    where pk.TABLE_NAME = @TableName
    and CONSTRAINT_TYPE = 'PRIMARY KEY'
    and c.TABLE_NAME = pk.TABLE_NAME
    and c.CONSTRAINT_NAME = pk.CONSTRAINT_NAME
    
    SET @PKSelect = (SELECT CAST(@PKFieldSelect AS varchar(100)) + ' ' + CAST(@PKValueSelect AS varchar(100)))
    exec sp_executesql @PKSelect 
    print(@PKSelect)
    
    
    --SELECT @PKSelect = @PKFieldSelect + @PKValueSelect
    
    
    IF @PKCols IS NULL
    BEGIN
           RAISERROR('no PK on table %s', 16, -1, @TableName)
           RETURN
    END
    
    select @sql='insert  Audit (Type,TableName,PK,OldValue,NewValue,UserName,UpdateDate)   
    select ''' + @Type + ''',''' 
           + @TableName + ''',' + @PKSelect    
           + ',''' + @OldValue+ ''''
           + ',''' + @NewValue+ ''''
           + ',''' + @UserName + ''''
           + ',''' + @UpdateDate+ ''''
           + ' from #ins i full outer join #del d'
           + @PKCols
                   EXEC (@sql)
        END
    GO
    

0 个答案:

没有答案