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

使用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块时候会出现错误。

转载于:https://www.cnblogs.com/OliverQin/p/5059052.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值