案例问题中陈述
问题描述:
我VE下面这个简单的case语句案例问题中陈述
CASE WHEN @SPOID is not null THEN
(176)
ELSE
(SELECT designationID from setupdesignations where reportto = 99)
END
当本声明的其他条款执行它给了我下面的错误
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
我正在使用本声明IN
子句我期望在其他情况下返回多个结果,但它给了我上述错误。如果我删除case语句并执行查询中的else部分,那么我得到预期的结果
答
您不能在CASE匹配中使用子查询作为表达式,它需要单个值。反而如何;
where
(@SPOID is not null and infield = 176)
or
(@SPOID is null and infield in (SELECT designationID from setupdesignations where reportto = 99))
答
虽然使用的是IN
子句,CASE
语句只允许一个行作为return语句。
你必须想出另一种方式来做到这一点。
答
试着改变你的子句:
... in
(SELECT designationID
from setupdesignations
where reportto = 99 and @SPOID is not null
UNION
SELECT 176 where @SPOID is null)
这仅仅是一个值。无论从这里返回什么都应该在上面查询的IN块中 – Tassadaque 2012-01-11 11:44:43