SQL Server 2017触发器,用于将旧值作为新值用于AFTER UPDATE。

huangapple go评论64阅读模式
英文:

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

huangapple
  • 本文由 发表于 2020年1月6日 22:33:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613899.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定