【问题标题】:Delete trigger to insert entry into table删除触发器以将条目插入表中
【发布时间】:2016-05-10 21:07:13
【问题描述】:

在我的DEV_SQL 数据库中创建了一个Customers_Audit 表,其中包含以下列:

[AuditID] int IDENTITY
[CustomerID] nchar(5)
[UserName] nvarchar(50)
[DeleteDate] smalldatetime

我正在尝试编写一个删除触发器,以在每次从Customers 表中删除一行时向Customers_Audit 表中插入一个条目,需要一些帮助哦聪明的人!

【问题讨论】:

标签: sql sql-server tsql triggers


【解决方案1】:

您需要这样的东西 - 只需将行插入您的 AFTER DELETE 触发器中的 Customers_Audit 并根据需要设置固定值:

CREATE TRIGGER trgAfterDelete 
ON dbo.Customer
AFTER DELETE
AS 
    -- insert into the audit table - explicitly specifying the column names to use
    INSERT INTO dbo.Customers_Audit(CustomerId, UserName, DeleteDate)
        -- make sure to respect the fact that the "Deleted" table might
        -- contain *multiple* rows if your DELETE statement deleted 
        -- more than a single row - use proper set-based code!
        SELECT
            d.CustomerId, d.UserName, SYSDATETIME()
        FROM 
            Deleted d

【讨论】:

    猜你喜欢
    • 2019-07-30
    • 1970-01-01
    • 1970-01-01
    • 2015-04-26
    • 1970-01-01
    • 2019-09-12
    • 2011-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多