使用多个条件更新 tsql 中的表

发布于 2024-10-08 10:49:08 字数 364 浏览 0 评论 0原文

如果我有一个表 MyTable,其中包含 a、b 和 c 列,它们都是整数。鉴于我想根据 b 和 c 的值更新所有 'a'。

Update MyTable set a = 2 where b = 1 and c = 1

已经太晚了,我一生都无法理解为什么这个声明不起作用,我是否错过了一些愚蠢的东西?

编辑一下,哎呀,忘记了错误。

“子查询返回超过 1 个值。当子查询跟在 =、!=、<、<=、>、>= 后面或子查询用作表达式时,这是不允许的。”

Edit2:这就是我正在使用的确切查询(不同的列名称)。原来桌子上有一个扳机坏了。我现在觉得有点傻,但还是谢谢你的帮助:)

If I have a table MyTable with columns a,b and c, which are ints. Given that I want to update all 'a's based on the values of b and c.

Update MyTable set a = 2 where b = 1 and c = 1

It's far too late, and I cannot for the life of me see why this statement doesn't work, am I missing something silly?

Edit, woops, forgot the error.

"Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression."

Edit2: That was the exact query I was using (different column names). Turns out there was a trigger on the table which was broken. I feel a little silly now, but thanks for the help anyway :)

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

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

发布评论

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

评论(1

胡渣熟男 2024-10-15 10:49:08

您发布的声明没有任何问题。错误在其他地方。

您可能发布了错误的查询吗?或者也许你过于简单化了?子查询看起来像这样:

UPDATE MyTable
SET a = 2 
WHERE b = 1 AND c = (SELECT c FROM MyTable2 WHERE id = 5)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ <--- subquery

一个无效的查询可能会给出您收到的错误消息,可能看起来像这样:

UPDATE MyTable
SET a = 2 
WHERE b = 1 AND c = (SELECT c, d FROM MyTable2 WHERE id = 5)

第二个查询无效,因为它返回两个值,但 = 运算符只允许与单个值进行比较价值。

解决方案是确保相等比较中使用的所有子查询仅返回由单列组成的单行。

There's nothing wrong with the statement you posted. The error is elsewhere.

Could you have posted the wrong query? Or perhaps you over-simplified it? A subquery looks something like this:

UPDATE MyTable
SET a = 2 
WHERE b = 1 AND c = (SELECT c FROM MyTable2 WHERE id = 5)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ <--- subquery

An invalid query that could give the error message you get could look like this:

UPDATE MyTable
SET a = 2 
WHERE b = 1 AND c = (SELECT c, d FROM MyTable2 WHERE id = 5)

The second query is invalid because it returns two values but the = operator only allows comparison to a single value.

The solution is to ensure that all subqueries used in equality comparisons only return a single row consisting of a single column.

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