案件陈述有问题

时间:2012-05-08 14:08:09

标签: sql sql-server-2008

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

当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

3 个答案:

答案 0 :(得分:4)

该案例是必须求值的表达式。您返回多个值的Select语句。

您似乎正在尝试使用Case,就像它是C#开关一样?如果是这种情况,那么你可能最好采用IF ELSE IF构造。

答案 1 :(得分: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

答案 2 :(得分: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的数据不匹配, 然后使用courseversions.*的空inner join过滤数据。

希望我有正确的理解。