您的位置:首页 > 数据库 > Oracle

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

2015-12-19 13:56 387 查看
使用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块时候会出现错误。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: