1

这几天我一直在和这个搏斗。我有一个表触发器,它为每个插入、更新或删除操作创建一个审计记录,并将其放置在相邻数据库的相应审计表中。此新审计记录包含源表主键值、操作类型、记录的 XML 快照、当前日期和当前用户。

上面列出的每条信息都是从 INSERTED \ DELETED 表中分配\计算的,并分配给参数值,然后将其传递给存储过程,该存储过程执行一条动态 SQL 以插入新记录。对于这个问题,动态 SQL 是一个简单的 INSERT 语句(如下所示):

        INSERT INTO [340bAudit].[aud].[TableName]
            (
                RecordID
                ,ActionType
                ,xml_snapshot
                ,ModifiedDate
                ,ModifiedBy
            )
            VALUES (@RecordPK 
                ,@action
                ,@data 
                ,GETDATE() 
                ,SYSTEM_USER 
                   )

只要只有一条记录被更新,此触发器似乎就可以正常工作,但是一旦发生在单个语句中更新多个记录的情况,我就会得到:

子查询返回超过 1 个值。当子查询跟随 =、!=、<、<=、>、>= 或子查询用作表达式时,这是不允许的。该语句已终止。

触发器如下所示:

ALTER TRIGGER [dbo].[RollOver_onUpdate] ON [dbo].[RollOver]
AFTER INSERT, update, DELETE
FOR EACH ROW

AS

BEGIN

/******** Audit *******/

DECLARE @TableName varchar(50)
        ,@RecordPK varchar(10)
        ,@action char(1)
        ,@data xml 

SET @TableName = 'RollOver'
SET @RecordPK = (SELECT DISTINCT RollOverID FROM INSERTED)

SET @action = 'I'; -- Set Action to Insert by default.
IF EXISTS (SELECT * FROM DELETED)
    BEGIN
        SET @action =
            CASE
                WHEN EXISTS (SELECT * FROM INSERTED)
                     THEN 'U' -- Set Action to Updated.
            ELSE 'D' -- Set Action to Deleted.       
            END
    END

SET @data = CASE WHEN @action <> 'D' THEN (SELECT'<rows>' + (SELECT * FROM INSERTED i FOR xml PATH) + '</rows>')
                 ELSE (SELECT'<rows>' + (SELECT * FROM DELETED d FOR xml PATH) + '</rows>')
            END

--Execute Audit Record Creation
EXECUTE sp_CreateAuditRecord    @Table = @TableName
                                ,@RecordID = @RecordPK
                                ,@ActionType = @action
                                ,@XML = @data


END

在这种情况下,我可以更改此触发器以按行执行吗?如果没有,我从这里去哪里?

我非常希望保留动态 SQL 的灵活性,因为我数据库中的每个表都使用相同的审计逻辑,这是第一个也是唯一一个出现问题的表,我认为这是由于“使用' 这张表。

4

1 回答 1

0

这就是我想出的。它绝对不是最优雅的代码,但它可以工作并解决我的问题。感谢您的回复。我会投票赞成,但没有人发布答案让我接受。

/******** Audit *******/

DECLARE @action char(1)

SET @action = 'I'; -- Set Action to Insert by default.

        IF @@ROWCOUNT = 0
        RETURN

        IF EXISTS (SELECT * FROM DELETED)
            BEGIN
                SET @action =   CASE WHEN EXISTS (SELECT * FROM INSERTED)
                                        THEN 'U' -- Set Action to Updated.
                                    ELSE 'D' -- Set Action to Deleted.       
                                 END 

                INSERT INTO [340bAudit].[aud].[Orders]
                    (
                        RecordID
                        ,ActionType
                        ,xml_snapshot
                        ,ModifiedDate
                        ,ModifiedBy
                    )
                SELECT d.OrderID,
                       CASE WHEN EXISTS (SELECT * FROM INSERTED)
                                        THEN 'U' -- Set Action to Updated.
                                    ELSE 'D' -- Set Action to Deleted.       
                                 END ,
                       CASE WHEN @action <> 'D' THEN (SELECT'<rows>' + (SELECT * FROM INSERTED i FOR xml PATH) + '</rows>')
                            ELSE (SELECT'<rows>' + (SELECT * FROM DELETED d FOR xml PATH) + '</rows>') END,
                        GETDATE(), 
                        SYSTEM_USER 
                FROM DELETED d 
            END

        -- INSERTED Order Records
        INSERT INTO [340bAudit].[aud].[Orders]
            (
                RecordID
                ,ActionType
                ,xml_snapshot
                ,ModifiedDate
                ,ModifiedBy
            )
        SELECT i.OrderID,
                @action,
                (SELECT'<rows>' + (SELECT * FROM INSERTED i FOR xml PATH) + '</rows>'),
                GETDATE(), 
                SYSTEM_USER 
        FROM INSERTED i 
于 2013-08-30T19:30:38.723 回答