案例表达抛出异常

时间:2015-01-06 04:12:46

标签: oracle case-expression

请考虑以下代码:

 Select FLAGS,
 CASE FLAGS 
 WHEN 0 THEN "10000000"
 WHEN 1 THEN "01000000"
 WHEN 2 THEN "00100000"
 WHEN 3 THEN "00010000"
 WHEN 4 THEN "00001000"
 WHEN 5 THEN "00000100"
 WHEN 6 THEN "00000010"
 WHEN 7 THEN "00000001"
 ELSE "00000000"
 END AS Test-W 
 FROM V_TEST

以上陈述引发错误:

ORA-00923: FROM keyword not found where expected
 00923. 00000 -  "FROM keyword not found where expected"
 *Cause:    
 *Action: Error at Line: 14 Column: 17

我的表名为V_TEST,列名为FLAGS。我在这里做错了什么?

2 个答案:

答案 0 :(得分:2)

使用单引号`作为文字表达式

列Test-W需要包含在dobule引用

Select FLAGS,
 CASE FLAGS 
 WHEN 0 THEN '10000000'
 WHEN 1 THEN '01000000'
 WHEN 2 THEN '00100000'
 WHEN 3 THEN '00010000'
 WHEN 4 THEN '00001000'
 WHEN 5 THEN '00000100'
 WHEN 6 THEN '00000010'
 WHEN 7 THEN '00000001'
 ELSE '00000000'
 END as "Test-W"
FROM V_TEST

答案 1 :(得分:1)

当然,CASE冗长且易于理解。您也可以使用DECODE -

编写相同的查询
SELECT FLAGS, 
       DECODE(FLAGS, 
       0 , '10000000',
       1 , '01000000',
       2 , '00100000',
       3 , '00010000',
       4 , '00001000',
       5 , '00000100',
       6 , '00000010',
       7 , '00000001',
           '00000000')
  AS TEST-W 
 FROM V_TEST
/