带有in语句的SQL语句中的大小写问题
我有以下简单的案例陈述带有in语句的SQL语句中的大小写问题,sql,sql-server,sql-server-2008,tsql,Sql,Sql Server,Sql Server 2008,Tsql,我有以下简单的案例陈述 CASE WHEN @SPOID is not null THEN (176) ELSE (SELECT designationID from setupdesignations where reportto = 99) END 当这个语句的else子句执行时,它给出了以下错误 Subquery returned more than 1 value. This is not permitte
CASE WHEN @SPOID is not null THEN
(176)
ELSE
(SELECT designationID from setupdesignations where reportto = 99)
END
当这个语句的else子句执行时,它给出了以下错误
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
子查询返回了多个值。当子查询在=、!=、=或者当子查询用作表达式时。
我在子句中的
中使用了此语句,我希望在其他情况下返回多个结果,但它给了我上述错误。如果我删除case语句并执行查询中的else部分,那么我将得到预期的结果您不能在case匹配中使用subselect作为表达式,它只需要一个值。相反,如何
where
(@SPOID is not null and infield = 176)
or
(@SPOID is null and infield in (SELECT designationID from setupdesignations where reportto = 99))
尽管在
子句中使用了,但CASE
语句只允许一行作为返回语句
您必须找到另一种方法来实现它。您可以尝试使用动态查询来实现最终目标。以下链接可能会为您指明正确的方向-查看最后一篇文章:尝试将in子句更改为:
... in
(SELECT designationID
from setupdesignations
where reportto = 99 and @SPOID is not null
UNION
SELECT 176 where @SPOID is null)
这只是一个值。无论从这里返回什么,都应该放在上面查询的in块中