1

下面是我为 SQL 数据库中的表创建的触发器(我们正在运行 SQL Server 2008)。我们将批量进行更新、插入和删除,因此我们创建了这个触发器和一个“存储”表 (TransactionLog) 来捕获所有活动。到目前为止,此触发器非常适用于插入和更新。所有记录都以所有适当的值出现在“存储”表中。

但是,当我们尝试删除多于一条记录时,就会出现问题。此触发器捕获并发送到“存储”表的唯一记录是最后删除的记录。所有其他人都会迷路。

CREATE TRIGGER [dbo].[trg_Agent_ALL] 
ON [dbo].[Agent]

FOR INSERT, UPDATE, DELETE
AS
BEGIN

--TransactionLog variables
DECLARE @tableName char(25) = 'Agent'
DECLARE @transDate datetime 
DECLARE @lastChangeOperator char(6) 
DECLARE @tableString char(255)
DECLARE @action char(1) = 'I'
DECLARE @userId char(25)

--Trigger table variables
DECLARE @sNumber char(10)
DECLARE @controlId char(3)
DECLARE @entityType char(1)
DECLARE @firstName nvarchar(50)
DECLARE @lastName nvarchar(50)
DECLARE @suffix nvarchar(10)
DECLARE @corpName nvarchar(100)


IF EXISTS (SELECT * FROM deleted)
    BEGIN
        SET @action = 
            CASE    
                WHEN EXISTS (SELECT * FROM inserted) THEN 'U'
                ELSE 'D'
            END
    END

IF @action = 'D'
    BEGIN 
        SELECT @sNumber = sNumber, @lastChangeOperator = LastChangeOperator, @transDate = LastChangeDate, @entityType = EntityType, 
        @firstName = FirstName, @lastName = LastName, @suffix = NameSuffix, @corpName = CorporateName, @controlId = ControlId
        FROM deleted

        IF @firstName IS NULL SET @firstName = 'NULL'
        IF @lastName IS NULL SET @lastName = 'NULL'
        IF @suffix IS NULL SET @suffix = 'NULL'
        IF @corpName IS NULL SET @corpName = 'NULL'
        IF @controlId IS NULL SET @controlId = 'NULL'

        SET @tableString = 'sNum:' + @sNumber + ' ' + 'EntType:' + @entityType + ' ' + 'Fname:' + @firstName + ' ' + 'Lname:' + @lastname + ' ' + 'suf:' + @suffix +
            ' ' + 'crpName:' + @corpName + ' ' + 'crlId:' + @controlId
    END
ELSE
    BEGIN
        SELECT @sNumber = SymetraNumber, @lastChangeOperator = LastChangeOperator, @transDate = LastChangeDate, @entityType = EntityType, 
        @firstName = FirstName, @lastName = LastName, @suffix = NameSuffix, @corpName = CorporateName, @controlId = ControlId
        FROM inserted

        IF @firstName IS NULL SET @firstName = 'NULL'
        IF @lastName IS NULL SET @lastName = 'NULL'
        IF @suffix IS NULL SET @suffix = 'NULL'
        IF @corpName IS NULL SET @corpName = 'NULL'
        IF @controlId IS NULL SET @controlId = 'NULL'

        SET @tableString = 'sNum:' + @sNumber + ' ' + 'EntType:' + @entityType + ' ' + 'Fname:' + @firstName + ' ' + 'Lname:' + @lastname + ' ' + 'suf:' + @suffix +
            ' ' + 'crpName:' + @corpName + 'crlId:' + @controlId
    END

INSERT INTO TransactionLog (TransactionDate, Operator, TableName, Action, TableString, UserId)
VALUES (@transDate, 'Op', @tableName, @action, @tableString, @lastChangeOperator)

END

根据下面的评论,我更改了删除部分中的 SQL 代码。硬编码的值似乎有效,但是我将它们放在那里的主要原因只是为了显示这些值是我需要这些特定列的值。我在上面的代码中使用这些值设置了变量(请参阅 DECLARE 语句)。但是,这给了我以下错误消息:

 Conversion failed when converting the varchar value 'P' to data type int.

此错误来自内部 SELECT 语句中的 EntityType 属性。让我感到困惑的是,该列的数据类型设置为 char(1),而 TableString 列(连接值的目标)的数据类型为 nvarchar(255)。不知道“int”来自哪里......

IF @action = 'D'
    BEGIN   
        INSERT INTO TransactionLog (TransactionDate, Operator, TableName, Action, TableString, UserId)
        SELECT LastChangeDate, 'Op', 'Agent', 'D', 
        (SELECT CAST(CAST(sNumber as nvarchar) + ' ' + EntityType + ' ' + ISNULL(FirstName, ' ') + ' ' + ISNULL(LastName, ' ') + ' ' + ISNULL(NameSuffix, ' ') + ' ' + 
                ISNULL(CorporateName, ' ' ) + ' ' + ISNULL(CAST(ControlId as nvarchar), ' ') AS nvarchar) as TableString
        FROM deleted), LastChangeOperator
        FROM deleted
    END
ELSE

编辑

通过将 sNumber 和 controlId 字段转换为 nvarchar,我能够跳过我之前的错误消息。现在,但是我收到下面发布的不同错误消息:

 Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

最终编辑

像这样更改子查询允许我将多个已删除的记录返回到 Audit 表中,因为我一次只请求一条记录。

IF @action = 'D'
    BEGIN 
        INSERT INTO TransactionLog (TransactionDate, Operator, TableName, Action, TableString, UserId)
            SELECT LastChangeDate, 'Op', 'Agent', 'D', 
            CAST('SymNum:' + CAST(SymetraNumber as nvarchar(30)) + ' entType:' + EntityType + ' Fname:' + ISNULL(FirstName, 'NULL') + ' Lname:' + ISNULL(LastName, 'NULL') +
             ' suff:' + ISNULL(NameSuffix, 'NULL') + ' corpName:' + ISNULL(CorporateName, 'NULL' ) + ' ctrlId:' + ISNULL(CAST(ControlId as nvarchar(30)), 'NULL') AS nvarchar(30)) as TableString
            , LastChangeOperator
        FROM deleted
    END
ELSE 
4

1 回答 1

3

select @sNumber = sNumber, ..语法将为每一行覆盖一次变量,因此您最终只会得到最后一行的值。

然后你也做一个insert into ... values (...),它只能插入一行。

您应该尝试以以下形式重写它:

insert into TransactionLog ( ... )
select sNumber, ... from deleted

您可以使用ISNULL(lastname, 'NULL')而不是您的 if 语句。

于 2012-09-03T02:19:37.197 回答