使用多个条件更新 tsql 中的表
如果我有一个表 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您发布的声明没有任何问题。错误在其他地方。
您可能发布了错误的查询吗?或者也许你过于简单化了?子查询看起来像这样:
一个无效的查询可能会给出您收到的错误消息,可能看起来像这样:
第二个查询无效,因为它返回两个值,但
=
运算符只允许与单个值进行比较价值。解决方案是确保相等比较中使用的所有子查询仅返回由单列组成的单行。
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:
An invalid query that could give the error message you get could look like this:
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.