0

我有一个查询,当我运行它时,我收到错误消息ORA-00907: missing parenthesis。当我用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

我在这里做错了什么?

4

3 回答 3

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
于 2013-08-28T09:32:36.450 回答
3

case is an expression, not a predicate (i.e condition) : it 'returns' a typed value and can not contain predicate as result (in the then parts). In your case (assuming else -1 means 'no match') :

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

although I think it would be simpler to replace it with :

AND (
     (strfldvar = 'BROKEN_ARROW' AND x = g) 
  OR (strfldvar = 'BROKEN_BOX' AND y = g)
)
于 2013-08-28T09:42:25.783 回答
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
于 2013-08-28T09:35:04.983 回答