添加新行时触发

时间:2017-08-16 01:40:03

标签: sql-server triggers alert

我想在我的Vendor表中添加新行时显示触发器。我到目前为止所写的查询是:

CREATE TRIGGER tVendorAdded
ON Vendors
AFTER UPDATE
AS
BEGIN
    INSERT INTO AlertTable
       SELECT  
           VendorID, VendorName, GETDATE(),
           'Vendor Added', 'New vendor, ' + VendorName + ', added'
       FROM 
           inserted
       WHERE --
END
GO

我要在where子句中添加什么才能实现此目的?我应该尝试一些不同/更好的东西吗?

1 个答案:

答案 0 :(得分:1)

您不需要WHERE条款:

CREATE TRIGGER tVendorAdded ON Vendors
AFTER UPDATE AS
BEGIN
    INSERT INTO AlertTable (VendorId, VendorName, ?, ?, ?)
        SELECT VendorID, VendorName, getdate(), 'Vendor Added',
              'New vendor, ' + VendorName + ', added'
        FROM inserted;
END;

您应该在INSERT语句中包含列名。 ?用于表示相应列的名称。