Oracle CASE时-ORA-00936:缺少表达式

时间:2019-09-24 19:39:03

标签: oracle plsql

我是oracle新手,下面是我的SQL。

SELECT * FROM TABLE1 WHERE COLUMN1 = 'YES'
AND COLUMN2 IN (


CASE WHEN EXISTS(SELECT * FROM TABLE1 WHERE COLUMN1 = 'YES' AND COLUMN2 NOT LIKE '%NO%') 
 THEN 
   SELECT COLUMN2 FROM TABLE1 WHERE COLUMN1 = 'YES' AND COLUMN2 NOT LIKE '%YES%'
 ELSE 
   SELECT COLUMN2 FROM TABLE1 WHERE COLUMN1 = 'YES' AND COLUMN2 NOT LIKE '%YES%'  END)

它给出ORA-00936:then语句缺少表达。我在做什么错了?

1 个答案:

答案 0 :(得分:3)

THENELSE之后的子查询必须用括号括起来:

SELECT * FROM TABLE1 WHERE COLUMN1 = 'YES'
AND COLUMN2 IN (
  CASE 
    WHEN EXISTS (SELECT * FROM TABLE1 WHERE COLUMN1 = 'YES' AND COLUMN2 NOT LIKE '%NO%') 
      THEN (SELECT COLUMN2 FROM TABLE1 WHERE COLUMN1 = 'YES' AND COLUMN2 NOT LIKE '%YES%')
    ELSE (SELECT COLUMN2 FROM TABLE1 WHERE COLUMN1 = 'YES' AND COLUMN2 NOT LIKE '%YES%')
  END
)

仅当这些子查询返回的行数不超过1行时,此方法才有效。
此外,两个子查询都相同。这是错字吗?
由于IN仅返回1个值,因此=可以更改为CASE