下面是在我们的一个 SQL 表上用于任何插入/更新操作的触发器。99/100 次这个触发器工作得很好,但是我们时不时地收到这个错误消息:
无法将值 NULL 插入到列“TransactionDate”、表“AgentResourcesU01.dbo.TransactionLog”中;列不允许空值。
插入失败。该语句已终止。
从 Insert 语句中可以看出,我们的事务日志表中的列是TransactionDate, Operator, TableName, Action, TableString
和UserId
。我在开始的 SELECT 语句中设置了变量@transDate
,因此在我看来,除非是坏数据进入,否则 NULL 应该没有办法进入那里。
有什么想法吗?
BEGIN
SELECT @symetraNumber = 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 = 'SymNum:' + @symetraNumber + ' EntType:' + @entityType + ' Fname:' + @firstName + ' Lname:' + @lastname + ' Suff:' + @suffix +
' CorpName:' + @corpName + ' ctrlId:' + @controlId
INSERT INTO TransactionLog (TransactionDate, Operator, TableName, Action, TableString, UserId)
VALUES (@transDate, 'Op', @tableName, @action, @tableString, @lastChangeOperator)
END