下面是我为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
答案 0 :(得分:2)
select @sNumber = sNumber, ..
语法将为每一行覆盖一次变量,因此最终只能使用最后一行的值。
然后你还做了insert into ... values (...)
,它只能插入一行。
您应该尝试以下列形式重写它:
insert into TransactionLog ( ... )
select sNumber, ... from deleted
您可以使用ISNULL(lastname, 'NULL')
代替if语句。