SQL Server:触发错误客户

时间:2015-03-20 04:52:45

标签: sql sql-server triggers

出于某种原因,我的触发器无法正常工作。我试图创建一个在客户付费时更新的触发器。

以下是代码:

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的列名无效。我非常困惑:(

1 个答案:

答案 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;