Oracle中case when和decode的用法

case when 在不同条件需要有不同返回值的情况下使用非常方便,可以在给变量赋值时使用,也可以在select查询语句中使用。
case搜索语句格式:

case
 when 条件1 then 返回值1
 when 条件2 then 返回值2
 ...
 else 返回值N 

end;


示例:
declare
  i   integer;
  str varchar2(20);
begin
  i   := 3;
  str := case
           when i = 1 then 'a'
           when i = 2 then 'b'
           when i = 3 then 'c'
         end ;
  dbms_output.put_line(str);
end;

 

case表达是语句格式:

case 条件
 when 值1 then 返回值1
 when 值2 then 返回值2
 ...
 else 返回值N
end;


示例:
declare
  i   integer;
  str varchar2(20);
begin
  i   := 3;
  str := case i
           when 1 then 'a'
           when 2 then 'b'
           when 3 then 'c'
         end;
  dbms_output.put_line(str);
end;

 

在sql语句中使用case when的格式:
select case 条件
 when 值1 then 返回值1
 when 值2 then 返回值2
 ...
 else 返回值N
      end [case] from...


select case
 when 条件1 then 返回值1
 when 条件2 then 返回值2
 ...
 else 返回值N
      end [case] from ...


示例:
select case dtype when 'factory' then '炼厂' when 'storage' then '油库' when 'pipeline' then '管道' else '其他' end case from acc_domain ;
select case when dtype='factory' then '炼厂' when dtype='storage' then '油库' when dtype='pipeline' then '管道' else '其他' end case from acc_domain ;

其中select case 条件
 when 值1 then 返回值1
 when 值2 then 返回值2
 ...
 else 返回值N
      end [case] from... 的功能和decode类似。

 

decode的使用格式:decode(变量或表达式,值1,返回值1,值2,返回值2,...,默认值)

另外case when和decode可以用来做列转行转换。
--列转行
create table S(
       Name nvarchar2(10),
       Course nvarchar2(10),
       Grade int
);

insert into S(Name, Course, Grade) values('张三', '语文', 80);
insert into S(Name, Course, Grade) values('张三', '数学', 86);
insert into S(Name, Course, Grade) values('张三', '英语', 75);
insert into S(Name, Course, Grade) values('李四', '语文', 78);
insert into S(Name, Course, Grade) values('李四', '数学', 85);
insert into S(Name, Course, Grade) values('李四', '英语', 70);

select * from S;


select Name,
       sum(decode(Course, '语文', Grade, 0)) 语文,
       sum(decode(Course, '数学', Grade, 0)) 数学,
       sum(decode(Course, '英语', Grade, 0)) 英语
from S
group by Name;

 

今天使用犯了个小错误,折腾了半天,在此记录。(2012年4月14日添加

   select case 1 
         when 1 then '未验收'
         when 2 then '已验收'
         else '未确认' end case from dual;--正确

但是想给字段起个别名PL/SQL却报了“ORA-00923:未找到要求的FROM关键字”错误,但from关键字明明有啊,调试了半天发现是 "end case"惹的祸,将“end case”换成“case”问题解决:

  select case 1 
         when 1 then '未验收'
         when 2 then '已验收'
         else '未确认' end status from dual;--起个别名

为了进一步搞清楚“end”和“end case”的使用区别自己做了两个示例:

-- Created on 2012/4/14 by CUISEA 
declare 
  i integer;
  s varchar2(100);
begin
  i:=2;
  s:=case i 
         when 1 then '未验收'
         when 2 then '已验收'
         else '未确认' end ;--必须使用“end”,不能只用“end case”
  dbms_output.put_line(s);
end;
-- Created on 2012/4/14 by CUISEA 
declare 
  i integer;
  s varchar2(100);
begin
  i:=2;
  case i 
         when 1 then s:='未验收';
         when 2 then s:='已验收';
         else s:='未确认';
         end case;--必须使用“end case”,不能只用“end”
  dbms_output.put_line(s);
end;

这说明在表达式中要使用“end”,而在PL/SQL过程判断中需要使用“end case”。但这也不是绝对的,使用的时候注意点就行了。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值