SQL 触发器一次 1 行

发布于 2024-08-12 03:20:44 字数 364 浏览 3 评论 0原文

我正在创建一个如下所示的更新触发器(SQL Server 2005):

该行的状态列是 23 还是 25,请勿更新它。不然更新一下吧这很简单。我正在尝试

OldState = (Select State from Deleted)

If OldState in (25,23)
   Update it --how to do it easily?
else
   dont do nothing for this row

问题是触发器是用所有更新的行调用的,所以删除是一个集合,这意味着第一条指令将不起作用,因为它试图仅获取 1 个值,但它得到了一个集合

。这么简单,我错过了什么吗?

非常感谢

I'm creating an update trigger that goes like this (SQL Server 2005):

Is the state column of the row is 23 or 25 don't update it. Else update it. It's very simple. I'm trying

OldState = (Select State from Deleted)

If OldState in (25,23)
   Update it --how to do it easily?
else
   dont do nothing for this row

The problem is that the trigger is called with all the updated rows, so deleted is a set, that means the first instruction won't work because it's trying to get only 1 value and it gets a set..

It's something so simple, am I missing something?

Thank you very much

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

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

发布评论

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

评论(1

高冷爸爸 2024-08-19 03:20:44

此代码假设:

  • 链接“旧”和“新”行的键保持不变,
  • 您需要简单的更新后处理

示例:

UPDATE
   M    --yes, this is correct
SET
   SomeCol = SomeValue,
   ...
FROM
   MyTable M
   JOIN
   DELETED D ON M.KeyCol = D.KeyCol
WHERE
   D.State IN (23, 25)

This code assumes:

  • the key stays the same to link "old" and "new" rows
  • you need simple post-update processing

Example:

UPDATE
   M    --yes, this is correct
SET
   SomeCol = SomeValue,
   ...
FROM
   MyTable M
   JOIN
   DELETED D ON M.KeyCol = D.KeyCol
WHERE
   D.State IN (23, 25)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文