case when else使用方法
(2012-03-07 13:43:59)
标签:
杂谈 |
分类: oracleadmin |
case when else 基本结构:
case n
when 2 then Action2;
when 3 then Action3;
else
end;
示例一:
SELECT col1, col2,
FROM table_51xit
case when else语法要点说明如下:
1、以CASE开头,以END结尾
2、分支中WHEN 后跟条件,THEN为显示结果
3、ELSE 为除此之外的默认情况,类似于高级语言程序中switch case的default,可以不加
4、END 后跟别名
示例二:更复杂的示例代码:
select d.districtcode,(case when (substr(d.districtcode,3,6) =
'0000') then ('100000'||substr(d.districtcode,1,2))
not exists
(
select 1 From vcmunitinfo u where
u.unitdistrictcode=d.districtcode
and u.unitcode=(case when (substr(d.districtcode,3,6) = '0000')
then ('100000'||substr(d.districtcode,1,2))
)
示例二:
===============一个完整的实例简要介绍case函数的用法
1.创建测试表:
DROP SEQUENCE student_sequence;
CREATE SEQUENCE student_sequence
DROP TABLE UserInfo;
CREATE TABLE UserInfo (