SQL Server 2017触发器将旧值作为AFTER UPDATE的新值

问题描述 投票:0回答:1

我使用SQL Server 2017 Management Studio。我需要trigg_1的触发器(AFTER UPDATE),该触发器在col_1列中插入旧值而不是新值。我将使用新值手动更改列col_1,触发器需要将旧值更改为col_1

CREATE TRIGGER trigg_1 
ON table_1 
AFTER UPDATE
AS 
BEGIN

END
GO
sql-server database triggers database-trigger
1个回答
1
投票

尝试一下:

CREATE TRIGGER trigg_1 
   ON  table_1 
   AFTER UPDATE
AS 
BEGIN

    UPDATE  T2
    SET     T2.COL_1 = T1.COL_1
    FROM    DELETED T1
    INNER JOIN TABLE_1 T2 ON T2.ID = T1.ID /* Here you must link by PK - I suggested PK contains only ID column. */

END
GO
© www.soinside.com 2019 - 2024. All rights reserved.