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

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

我使用MS SQL Management Studio 2017。我需要trigger(trigg_1)为AFTER UPDATE,它要插入column(col_1)旧值而不是新值。我将使用新值手动更改column col_1,触发器需要使用旧值更改col_1

CREATE TRIGGER trigg_1 
   ON  table_1 
   AFTER UPDATE
AS 
BEGIN


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

尝试一下:

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.