oracle 如何在 Case 语句中使用 If 条件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9477627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 03:59:49  来源:igfitidea点击:

How to use If condition inside a Case statement?

oracleif-statementcase

提问by Samurai

Below is my query. Is this righ?

以下是我的查询。这是对的吗?

SQL> select case when value in (1000) then null
2  when user in ('ABC') then user
3  when area in ('DENVER') then
4  if value = 2000 then 'Service1'
5  else value = 3000 then 'Service2'
6  end if
7  else null
8  end as num_code from service_usoc_ref;
if prin = 2000 then 'Omaha'
*
ERROR at line 4:
ORA-00905: missing keyword

Please help me.

请帮我。

回答by A.B.Cade

You can either put another case or use decode (as @madhu suggested):

您可以放置​​另一个案例或使用解码(如@madhu 建议的那样):

select case 
  when value in (1000) then null
  when user in ('ABC') then user
  when area in ('DENVER') then
    case when value = 2000 then 'Service1'
         when value = 3000 then 'Service2'
    end 
  else null
  end as num_code 
from service_usoc_ref;

回答by Maddy

This could help you

这可以帮助你

 select  case when value in (1000) then null
                         when user in ('ABC') then user
                       when area in ('DENVER') then
decode( value, 2000 , 'Service1',3000 , 'Service2', NULL)  num_code 
from service_usoc_ref;