SQL Server 而不是插入触发器 - 有没有一种简单的方法来修改单个列?

发布于 2024-08-30 19:06:26 字数 185 浏览 2 评论 0原文

鉴于SQL Server不允许在触发器中修改逻辑INSERTED和DELETED表,是否有一种简单的方法可以更改单个列的值而不必重新编写整个插入语句?

例如,我应用触发器的表有 20 列。我想修改 INSERTED 表中一列(每行)的值,然后将该行插入表中。我可以在不编写包含 19 列加上我要修改其值的一列的插入语句的情况下执行此操作吗?

Given that SQL Server does not allow modification of the logical INSERTED and DELETED tables in a trigger, is there an easy way to change the value of a single column and not have to re-write the entire insert statement?

For example, the table to which I am applying the trigger has 20 columns. I want to modify the value from the INSERTED table for one column (per row) and then insert that row into the table. Can I do so without writing an insert statement with 19 columns plus the one column whose value I'm modifying?

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

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

发布评论

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

评论(2

咿呀咿呀哟 2024-09-06 19:06:26

是的,只需使用“插入后”触发器,而不是“代替插入”触发器。

这样,该行就已经被插入到表中,您所要做的就是发出一个(精心设计的)更新语句,通常是通过内连接到主键字段上的 INSERTED 表。

Yes, just use an "after insert" trigger, instead of a "instead-of insert" trigger.

That way the row has already been inserted into the table, and all you have to do is issue a (carefully crafted) update statement, usually by inner-joining to the INSERTED table on the primary key field.

漆黑的白昼 2024-09-06 19:06:26

要向 BradC 的答案添加更多详细信息:

带有连接的更新的语法有点不稳定

Update table
  set columnA = inserted.columnA / 10
from table
inner join inserted on table.id = inserted.id

To add more detail to BradCs answer:

The syntax for an update with a join is a little wonky

Update table
  set columnA = inserted.columnA / 10
from table
inner join inserted on table.id = inserted.id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文