You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Oracle查询ORA-00905错误修复咨询:CASE表达式语法问题

修复ORA-00905错误的方案

错误原因分析

你的SQL语句存在两个核心问题:

  1. CASE表达式语法不完整:缺少END关键字,这是触发ORA-00905: missing keyword错误的直接原因,Oracle无法识别未闭合的CASE结构。
  2. 逻辑条件矛盾: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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 08:03:01