出于某种原因,我的触发器无法正常工作。我试图创建一个在客户付费时更新的触发器。
以下是代码:
Select * from customers
Select * from customerAudit
CREATE TRIGGER tr_customer_ForUpdate
ON customers
FOR UPDATE
AS
BEGIN
INSERT INTO customerAudit
SELECT
'Customer with ID = ' + Cast (customersID as NVARCHAR(5)) + ' paid at ' + cast(Getdate() as NVARCHAR(20))
FROM
updated;
END
updated
包含无效的对象名称,customersID
的列名无效。我非常困惑:(
答案 0 :(得分:1)
SQL Server触发器中的伪表称为Deleted
(对于UPDATE
的旧值)和Inserted
(对于新值,在UPDATE
之后 - 不是Updated
。
试试这个:
INSERT INTO customerAudit (ColumnName)
SELECT
'Customer with ID = ' + Cast (customersID as NVARCHAR(5)) + ' paid at ' + cast(Getdate() as NVARCHAR(20))
FROM
Inserted;