具有多行的 T-SQL 更新触发器

发布于 2024-09-07 14:11:02 字数 259 浏览 1 评论 0原文

考虑表 A 上的更新后触发器。 对于每次更新,触发器都应更新表 B 中的所有记录。 然后考虑这个查询:

UPDATE A SET X = Y 

显然有很多行被更新。更新后触发。 现在,如果触发器使用 inserted 表,并且您希望使用临时表 inserted 的每一行更新表 B,并且在 MSDN 中不建议这样做使用游标,你会怎么做?

谢谢

Consider a trigger after update on the table A.
For every update the trigger should update all the records in table B.
Then consider this query:

UPDATE A SET X = Y 

Apparently there are many rows updated. After the update the trigger takes place.
Now if the trigger would be using inserted table, and you would like to update the table B with every single row of the temporary table inserted, and in MSDN is not recommended to use cursors, how would you do that?

Thank you

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

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

发布评论

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

评论(2

你爱我像她 2024-09-14 14:11:02

我不知道你到底想在更新触发器中做什么,但你可以例如

UPDATE dbo.B
SET someColumn = i.Anothervalue
FROM Inserted i
WHERE b.Criteria = i.Criteria

或其他 - 你需要告诉我们更多关于你想对表 B 做什么!但绝对可以更新、插入或执行其他操作,而无需使用游标并处理 Inserted 表中的多行。

I don't know what exactly you want to do in your update trigger, but you could e.g.

UPDATE dbo.B
SET someColumn = i.Anothervalue
FROM Inserted i
WHERE b.Criteria = i.Criteria

or something else - you need to tell us a bit more about what it is you want to do with table B! But it's definitely possible to update, insert into or other things, without using a cursor and handling multiple rows from the Inserted table.

幽梦紫曦~ 2024-09-14 14:11:02

我将假设表 A 通过键与表 B 相关(必须假设,因为您没有发布任何详细信息)。

如果是这种情况,您可以使用子查询或带有 inserted 的联接来选择表 B 上需要更改的行。

UPDATE tableB B
SET B.colx = someValue
WHERE B.id IN
(
    SELECT b_id 
    FROM INSERTED
)

I will assume that table A is related to table B via a key (have to assume, as you posted no details).

If that is the case, you can use either sub-queries or joins with inserted to select the rows that need changing on table B.

UPDATE tableB B
SET B.colx = someValue
WHERE B.id IN
(
    SELECT b_id 
    FROM INSERTED
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文