0

我对这个案例陈述有疑问。我不知道我做错了什么,但我得到了错误:

当不使用 EXISTS 引入子查询时,选择列表中只能指定一个表达式。

我有一种情况,当字段等于一个值然后执行左外连接,但如果字段等于不同的值,则执行内连接。这是我的查询:

SELECT
case when oqt = '26' then

(Select qm.id_oqm, cast(isNull(id_eval, '') as varChar(50)) + ' - ' + qm.methodName as methodName, qm.methodName as actualMethod,cv.* FROM OQMethods QM left outer join courseversions cv on cv.evalid = QM.id_eval and cv.courselanguage = 'EN' and cv.courseactive='Y' and cv.id_cp > 0 WHERE QM.id_oqt in (SELECT tempoq.oqt FROM tempoq INNER JOIN OQMethods ON tempoq.oqt = OQMethods.id_oqt)and active = 1) END, case when oqt = '31' then (Select qm.id_oqm, cast(isNull(id_eval, '') as varChar(50)) + ' - ' + qm.methodName as methodName, qm.methodName as actualMethod,cv.* FROM OQMethods QM inner join courseversions cv on cv.evalid = QM.id_eval and cv.courselanguage = 'EN' and cv.courseactive='Y' and cv.id_cp > 0 where QM.id_oqt in (SELECT tempoq.oqt FROM tempoq INNER JOIN OQMethods ON tempoq.oqt = OQMethods.id_oqt) and active = 1) END from tempoq
4

3 回答 3

4

case 是一个必须计算为一个值的表达式。您拥有的 Select 语句返回多个值。

似乎您正在尝试像使用 C# 开关一样使用 Case?如果是这种情况,那么您可能会更好地进行IF ELSE IF建筑。

于 2012-05-08T14:12:31.570 回答
2

看起来您想要做这样的事情而不是使用 CASE 语句。

DECLARE @t int
-- This would contain your oqt value
SET @t =1


IF @t = 1 
BEGIN
   SELECT * FROM tableA
END
ELSE IF @t = 2
BEGIN
   SELECT * FROM TableB
END
于 2012-05-08T14:16:40.460 回答
0
Select qm.id_oqm, cast(isNull(id_eval, '') as varChar(50)) + ' - ' + qm.methodName as methodName, qm.methodName as actualMethod,cv.*
 FROM OQMethods QM 
 inner join tempoq on tempoq.oqt = QM.id_oqt
 left outer join courseversions cv on cv.evalid = QM.id_eval and cv.courselanguage = 'EN' and cv.courseactive='Y' and cv.id_cp > 0 
 WHERE active = 1 and (tempoq.oqt = '26' or (tempoq.oqt = '31' and courseversions.* is not null))

left outer join表示加入OQMethods甚至不匹配数据courseversions的数据,然后用null过滤courseversions.*数据inner join

希望我有正确的理解。

于 2012-05-08T14:45:33.483 回答