条款案件陈述; ORA-00907:缺少括号

时间:2013-08-28 09:28:13

标签: sql oracle

我有一个查询,当我运行它时,我收到错误消息 ORA-00907:缺少括号。当我用 x = g和 y = g和替换 CASE 语句时,它会按预期运行。

SELECT *
FROM
table1,
table2,
table3,
table4,
table5,
table6,
table7,
table8
WHERE
a = b and
c = d and
e = d and
CASE strfldvar
    WHEN 'BROKEN_ARROW' THEN (x = g)
    WHEN 'BROKEN_BOX'   THEN (y = g)
ELSE -1
end 
and
f = h and
i = j

我在这里做错了什么?

3 个答案:

答案 0 :(得分:3)

而不是

CASE strfldvar
    WHEN 'BROKEN_ARROW' THEN (x = g)
    WHEN 'BROKEN_BOX'   THEN (y = g)
ELSE -1

有这个:

x=CASE strfldvar WHEN 'BROKEN_ARROW' THEN g ELSE x END
y=CASE strfldvar WHEN 'BROKEN_ARROW' THEN g ELSE y END

答案 1 :(得分:3)

case是一个表达式,而不是谓词(即条件):它'返回'一个类型化的值,并且不能包含谓词作为结果(在then部分中)。在您的情况下(假设else -1表示“不匹配”):

AND g = CASE strfldvar
  WHEN 'BROKEN_ARROW' THEN x
  WHEN 'BROKEN_BOX'   THEN y 
  ELSE NULL -- never match, even if g is null
END

虽然我认为用以下内容替换它会更简单:

AND (
     (strfldvar = 'BROKEN_ARROW' AND x = g) 
  OR (strfldvar = 'BROKEN_BOX' AND y = g)
)

答案 2 :(得分:1)

我会替换

CASE strfldvar
    WHEN 'BROKEN_ARROW' THEN (x = g)
    WHEN 'BROKEN_BOX'   THEN (y = g)
ELSE -1

通过

(CASE WHEN strfldvar = 'BROKEN_ARROW' and x = g then 1 
      WHEN strfldvar = 'BROKEN_BOX' and y = g then 1
      ELSE -1
      END) = 1