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

相关文章:

  • 2022-02-10
  • 2022-01-12
  • 2022-12-23
  • 2021-07-08
  • 2022-01-25
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-01-27
  • 2021-07-26
  • 2021-06-30
  • 2021-06-30
  • 2021-04-12
相关资源
相似解决方案