Oracle查询ORA-00905错误修复咨询:CASE表达式语法问题
修复ORA-00905错误的方案
错误原因分析
你的SQL语句存在两个核心问题:
- CASE表达式语法不完整:缺少
END关键字,这是触发ORA-00905: missing keyword错误的直接原因,Oracle无法识别未闭合的CASE结构。 - 逻辑条件矛盾:
c.city is null and c.city!=null是永远不成立的矛盾条件,同时这种写法不符合CASE表达式中THEN子句的语法要求。
修复后的SQL(根据需求选择)
需求1:当客户名为'Sai'时筛选city为空的记录,其他客户筛选city不为空的记录
使用CASE表达式的正确写法:
select c.customer_id, c.customer_name, c.city from customers c where case when c.customer_name = 'Sai' then c.city is null else c.city is not null end;
或者更简洁的逻辑表达式写法(推荐):
select c.customer_id, c.customer_name, c.city from customers c where (c.customer_name = 'Sai' and c.city is null) or (c.customer_name != 'Sai' and c.city is not null);
需求2:仅筛选客户名为'Sai'且city为空的记录
直接简化逻辑,无需CASE:
select c.customer_id, c.customer_name, c.city from customers c where c.customer_name = 'Sai' and c.city is null;
注意事项
- 在Oracle中判断空值必须使用
is null或is not null,不能用=或!=,因为null不等于任何值(包括它自己)。 - CASE表达式在WHERE子句中使用时,必须确保每个分支返回可被评估为布尔值的结果,且必须以
END闭合。
内容的提问来源于stack exchange,提问作者user19471184
相关产品推荐
相关产品推荐

