简体   繁体   English

SQL Server子查询返回了多个值。 当子查询跟随(字符)或子查询用作表达式时,不允许这样做

[英]SQL Server Subquery returned more than 1 value. This is not permitted when the subquery follows (chars) or when the subquery is used as an expression

I am trying to update some fields based on their occurence. 我试图根据它们的出现更新一些字段。 If they only occur one time, I am updating some status field. 如果它们只出现一次,我正在更新一些状态字段。

My current code is as follows: 我目前的代码如下:

UPDATE table1
SET statusField = 1
WHERE someID = (
               SELECT someID
               FROM table1
               GROUP BY someID HAVING COUNT(*) = 1
               )

This returns an error like the one in the title: Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. 这将返回一个错误,如标题中的错误: Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

Is there any other, as easily readable/simple, solution to this? 还有其他易于阅读/简单的解决方案吗?

Use IN keyword instead of equals operator like so: 使用IN关键字而不是equals运算符,如下所示:

UPDATE table1
SET statusField = 1
WHERE someID IN (
           SELECT someID
           FROM table1
           GROUP BY someID HAVING COUNT(*) = 1
           )

Using = requires that exactly 1 result is returned by the subquery. 使用=要求子查询返回1个结果。 IN keyword works on a list. IN关键字适用于列表。

You should join your tables in the subselect. 您应该在子选择中加入表格。 It is possible to use 'in', but in your case I would use exists: 可以使用'in',但在你的情况下我会使用exists:

UPDATE table1 x
SET statusField = 1
WHERE exists (
               SELECT null
               FROM table1
               WHERE x.someID = someID
               GROUP BY someID 
               HAVING COUNT(*) = 1
               )

For better performance I would use this script instead (sqlserver-2008+): 为了获得更好的性能,我会使用这个脚本(sqlserver-2008 +):

;WITH x as
(
SELECT rc = count() over (partition by someID), statusField
FROM table1
)
UPDATE x
SET statusField = 1
WHERE rc = 1

Try this 尝试这个

Use Top 使用Top

UPDATE table1
SET statusField = 1
WHERE someID = (
               SELECT TOP 1 someID
               FROM table1
               GROUP BY someID HAVING COUNT(*) = 1
               )

Or you can use IN clause 或者你可以使用IN子句

UPDATE table1
SET statusField = 1
WHERE someID IN (
               SELECT someID
               FROM table1
               GROUP BY someID HAVING COUNT(*) = 1
               )

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 子查询返回超过 1 个值。 当子查询跟随......或当子查询用作表达式时,这是不允许的 - Subquery returned more than 1 value. This is not permitted when the subquery follows … or when the subquery is used as an expression 子查询返回的值超过1。 当子查询遵循&gt; =或将子查询用作表达式时,不允许这样做 - Subquery returned more than 1 value. This is not permitted when the subquery follows>= or when the subquery is used as an expression 子查询返回的值超过1。 当子查询遵循=,!=,&lt;,&lt;=,&gt;,&gt; =或用作表达式时,不允许使用 - Subquery returned more than 1 value. Not permitted when subquery follows =, !=, <, <= , >, >= or used as an expression 子查询返回的值超过1。 当子查询遵循=,!=,&lt;,&lt;=,&gt;,&gt; =…?时,不允许这样做。 - Subquery returned more than 1 value. This is not permitted when the subquery follows =,!=, <, <= , >, >= …? 子查询返回的值超过1。 当子查询遵循=时,这是不允许的 - Subquery returned more than 1 value. This is not permitted when the subquery follows = 子查询返回的值超过1。 当子查询跟随时,这是不允许的 - Subquery returned more than 1 value. This is not permitted when the subquery follows 子查询返回的值超过1。 当子查询遵循=,!=,&lt;,&lt;=,&gt;,&gt; =时,不允许这样做 - Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= 子查询返回的值超过1。 当子查询遵循=,!=,&lt;,&lt;=,&gt;,&gt; =时,不允许这样做 - Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= 子查询返回超过 1 个值。 当子查询跟随 =, ,=, &lt;, &lt;=, &gt;, &gt;= 或 - Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or 子查询返回的值超过1。 当子查询跟随=,!=时,不允许这样做, - Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=,
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM