如何使用更新触发器来更新另一个表?

发布于 2024-10-21 00:43:16 字数 489 浏览 1 评论 0原文

我是触发器新手,想要在更新列时创建触发器并使用该值更新另一个表。

我的表 1 包含年份列,如果应用程序更新该年份列,我需要使用同年的年份更新表 2。

ALTER TRIGGER [dbo].[trig_UpdateAnnualYear]
   ON  [dbo].[table1]
   AFTER UPDATE
AS 

if (UPDATE (intAnnualYear))   
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for trigger here

    Update table2 set AnnualYear = intAnnualYear where table2.ID = table1.ID
END

I am new to triggers and want to create a trigger on an update of a column and update another table with that value.

I have table1 with a year column and if the application updates that year column I need to update table 2 with the year the same year.

ALTER TRIGGER [dbo].[trig_UpdateAnnualYear]
   ON  [dbo].[table1]
   AFTER UPDATE
AS 

if (UPDATE (intAnnualYear))   
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for trigger here

    Update table2 set AnnualYear = intAnnualYear where table2.ID = table1.ID
END

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蘸点软妹酱 2024-10-28 00:43:16

您没有在触发器内引用table1。使用 inserted 伪表获取“after”值。另请记住,更新可能会影响多行。

因此,将当前的 update 语句替换为

UPDATE table2
SET    table2.annualyear = inserted.intannualyear
FROM   table2
       JOIN inserted
         ON table2.id = inserted.id  

You don't reference table1 inside the trigger. Use the inserted pseudo table to get the "after" values. Also remember that an update can affect multiple rows.

So replace your current update statement with

UPDATE table2
SET    table2.annualyear = inserted.intannualyear
FROM   table2
       JOIN inserted
         ON table2.id = inserted.id  
愚人国度 2024-10-28 00:43:16

如果涉及到intannualyear列,则只需更新table2中的记录即可。此外,这是 Martin 展示的跨两个表的替代 UPDATE 语法

IF UPDATE(intannualyear)
    UPDATE table2
    SET    annualyear = inserted.intannualyear
    FROM   inserted
    WHERE table2.id = inserted.id

You only need to update the records in table2 if the column intannualyear is involved. Also, this is an alternative UPDATE syntax across two tables from what Martin has shown

IF UPDATE(intannualyear)
    UPDATE table2
    SET    annualyear = inserted.intannualyear
    FROM   inserted
    WHERE table2.id = inserted.id
九命猫 2024-10-28 00:43:16

根据 这个问题,如果只有一个“下游”表,则另一种选择具有正确定义的外键关系将是级联更新。

According to this question, if there's only one "downstream" table then another option with a properly defined foreign key relation would be Cascaded update.

_蜘蛛 2024-10-28 00:43:16

为了补充上述答案,如果您必须检查多个列,您可以在插入和删除之间使用 INNER JOIN,或多次 UPDATE() 调用:

IF ( UPDATE(Col1) OR UPDATE(Col2) ) BEGIN ...

To supplement the above answers, if you have to check more than one column you can use a INNER JOIN between inserted and deleted, or several UPDATE() calls:

IF ( UPDATE(Col1) OR UPDATE(Col2) ) BEGIN ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文