码迷,mamicode.com
首页 > 数据库 > 详细

Oracle中是用case...when语句进行判断

时间:2015-12-19 14:57:59      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:

使用case...when语句进行判断,其语法格式如下:

 case<selector>

when<expression_1> then pl_sqlsentence_1;

when<expression_2> then pl_sqlsentence_2;

...

when<expression_n> then pl_sqlsentence_n;

[else plsql_sentence;]

end case;

具体例子如下:

declare 
 v_season int:=3;
 autoinfo varchar2(50);
begin
  case v_season 
    when 1 then 
      autoinfo :=v_season||季节包括1,2,3月份;  
    when 2 then 
      autoinfo :=v_season||季节包括4,5,6月份;  
    when 3 then 
      autoinfo :=v_season||季节包括7,8,9月份;
    when 4 then 
      autoinfo :=v_season||季节包括10,11,12月份;
  else
    autoinfo :=v_season||季节不合法;
  end case;
    dbms_output.put_line(autoinfo);
end;   

输出结果:

3季节包括7,8,9月份

 

在使用case...when 时候,只需要写一个case就ok,不可以写多个,错误写法如下:

declare 
 v_season int:=3;
 autoinfo varchar2(50);
begin
  case v_season 
    when 1 then 
      autoinfo :=v_season||季节包括1,2,3月份; 
  case v_season 
    when 2 then 
      autoinfo :=v_season||季节包括4,5,6月份; 
  case v_season 
    when 3 then 
      autoinfo :=v_season||季节包括7,8,9月份;
  case v_season
    when 4 then 
      autoinfo :=v_season||季节包括10,11,12月份;
  else
    autoinfo :=v_season||季节不合法;
  end case;
    dbms_output.put_line(autoinfo);
end;   

如果这样写的话,语法是错误的,在运行pl/sql块时候会出现错误。

Oracle中是用case...when语句进行判断

标签:

原文地址:http://www.cnblogs.com/OliverQin/p/5059052.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!