ORACLE SQL CASE 语句 - 出现错误

ORACLE SQL CASE statement - getting error

提问人:ownmurad 提问时间:11/19/2020 最后编辑:marc_sownmurad 更新时间:11/19/2020 访问量:228

问:

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

SQL Oracle 语法错误

评论

1赞 Thorsten Kettner 11/19/2020
旁注:是字符串列还是为什么要使用引号(等)?如果是数字列,则应使用数字,例如.zoc'51'WHEN ZOC = 51
0赞 Thorsten Kettner 11/19/2020
最好的解决方案当然是将区域字符串映射到 zoc 数字的区域表,但我想您知道这一点。

答:

1赞 juergen d 11/19/2020 #1
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;
0赞 Popeye 11/19/2020 #2

语句有两种写法。CASE..WHEN

  1. CASE WHEN COLUMN_NAME = VALUE1 THEN ... WHEN COLUMN_NAME = VALUE2 THEN .. END
  2. 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;
2赞 Thorsten Kettner 11/19/2020 #3

您正在混合两种语法。决定一个。

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 ...