具有多行的 T-SQL 更新触发器
考虑表 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道你到底想在更新触发器中做什么,但你可以例如
或其他 - 你需要告诉我们更多关于你想对表 B 做什么!但绝对可以更新、插入或执行其他操作,而无需使用游标并处理
Inserted
表中的多行。I don't know what exactly you want to do in your update trigger, but you could e.g.
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.我将假设表 A 通过键与表 B 相关(必须假设,因为您没有发布任何详细信息)。
如果是这种情况,您可以使用子查询或带有
inserted
的联接来选择表 B 上需要更改的行。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.