ORACLE SQL CASE 语句 - 出现错误
Posted
技术标签:
【中文标题】ORACLE SQL CASE 语句 - 出现错误【英文标题】:ORACLE SQL CASE statement - getting error 【发布时间】:2020-11-19 07:07:32 【问题描述】:SELECT
ZOC, UNIT,
CASE ZOC
WHEN ZOC = '51' THEN 'ZONE OPERATION KHULNA'
WHEN ZOC = '52' THEN 'ZONE OPERATION JESSORE'
WHEN XOC = '53' THEN 'ZONE OPERATION KUSHTIA'
END
FROM
GNGRB.BS_CLOSING
ORDER BY
ZOC;
我收到此错误:
ORA-00905:缺少关键字 00905. 00000 - “缺少关键字” *原因: *行动: 行错误:16 列:10
【问题讨论】:
附带说明:zoc
是字符串列还是为什么要使用引号('51'
等)?如果是数字列,则应使用数字,例如WHEN ZOC = 51
.
最好的解决方案当然是将区域字符串映射到区域编号的区域表,但我想你知道。
【参考方案1】:
您正在混合两种语法。决定一个。
CASE zoc WHEN ...
SELECT
zoc, unit,
CASE zoc
WHEN 51 THEN 'ZONE OPERATION KHULNA'
WHEN 52 THEN 'ZONE OPERATION JESSORE'
WHEN 53 THEN 'ZONE OPERATION KUSHTIA'
END
FROM ...
CASE WHEN ...
SELECT
zoc, unit,
CASE
WHEN zoc = 51 THEN 'ZONE OPERATION KHULNA'
WHEN zoc = 52 THEN 'ZONE OPERATION JESSORE'
WHEN zoc = 53 THEN 'ZONE OPERATION KUSHTIA'
END
FROM ...
【讨论】:
【参考方案2】:SELECT ZOC, UNIT, CASE WHEN ZOC = '51' THEN 'ZONE OPERATION KHULNA'
WHEN ZOC = '52' THEN 'ZONE OPERATION JESSORE'
WHEN ZOC = '53' THEN 'ZONE OPERATION KUSHTIA'
END
FROM GNGRB.BS_CLOSING
ORDER BY ZOC;
【讨论】:
【参考方案3】:CASE..WHEN
语句有两种写法。
CASE WHEN COLUMN_NAME = VALUE1 THEN ... WHEN COLUMN_NAME = VALUE2 THEN .. END
CASE COLUMN_NAME WHEN VALUE1 THEN ... WHEN VALUE2 THEN ... END
在你的情况下,你可以这样写:
SELECT ZOC,
UNIT,
CASE
--ZOC -- just remove this
WHEN ZOC = '51' THEN
'ZONE OPERATION KHULNA'
WHEN ZOC = '52' THEN
'ZONE OPERATION JESSORE'
WHEN ZOC = '53' THEN
'ZONE OPERATION KUSHTIA'
END
FROM GNGRB.BS_CLOSING
ORDER BY ZOC;
-- 或
SELECT ZOC,
UNIT,
CASE ZOC
WHEN '51' THEN
'ZONE OPERATION KHULNA'
WHEN '52' THEN
'ZONE OPERATION JESSORE'
WHEN '53' THEN
'ZONE OPERATION KUSHTIA'
END
FROM GNGRB.BS_CLOSING
ORDER BY ZOC;
【讨论】:
以上是关于ORACLE SQL CASE 语句 - 出现错误的主要内容,如果未能解决你的问题,请参考以下文章
Oracle SQL - 使用 Case 语句缺少关键字错误的更新查询