英文:
SQL Server 2017 trigger for old value as new value for AFTER UPDATE
问题
我使用SQL Server 2017管理工具。我需要一个触发器(trigg_1
)用于AFTER UPDATE
,它将在列col_1
中插入旧值而不是新值。我将手动更改列col_1
的新值,触发器需要将col_1
更改为旧值。
CREATE TRIGGER trigg_1
ON table_1
AFTER UPDATE
AS
BEGIN
END
GO
英文:
I use SQL Server 2017 Management Studio. I need a trigger (trigg_1
) for AFTER UPDATE
which inserts in column col_1
the old value instead of new value. I will manually change column col_1
with new value and trigger needs to change col_1
with old value.
CREATE TRIGGER trigg_1
ON table_1
AFTER UPDATE
AS
BEGIN
END
GO
答案1
得分: 1
尝试这个:
创建触发器 trigg_1
在 table_1 上
在更新后
作为
开始
更新 T2
设置 T2.COL_1 = T1.COL_1
来自 DELETED T1
内连接 TABLE_1 T2 ON T2.ID = T1.ID /* 这里你必须通过主键连接 - 我建议主键仅包含 ID 列 */
结束
GO
英文:
Try this:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论