SQL中的CASE语句可以完成很多有意义的事情,通过这个小文儿给大家展示一下使用CASE语句做一个自我介绍。
1.创建实验表T,并初始化数据。
sec@ora10g> create table t (x varchar2(8));
sec@ora10g> insert into t values ('Andy');
sec@ora10g> insert into t values ('HOU');
sec@ora10g> insert into t values ('Anna');
sec@ora10g> insert into t values ('TuTu');
sec@ora10g> insert into t values ('Secooler');
sec@ora10g> commit;
2.简单查看一下T表中的数据
sec@ora10g> select * from t;
X
--------
Andy
HOU
Anna
TuTu
Secooler
3.现在是使用CASE的自我介绍时间
sec@ora10g> select x, case
2 when x = 'Andy' then '<-- It''s my English name.'
3 when x = 'HOU' then '<-- It''s my family name.'
4 when x = 'Anna' then '<-- She is my wife.'
5 when x = 'TuTu' then '<-- He is my son. He is one year old now.'
6 when x = 'Secooler' then '<-- Yes, It''s me.'
7 else null
8 end
9 introduce
10 from t
11 /
X INTRODUCE
-------- -----------------------------------------
Andy <-- It's my English name.
HOU <-- It's my family name.
Anna <-- She is my wife.
TuTu <-- He is my son. He is one year old now.
Secooler <-- Yes, It's me.
4.有关CASE的官方文档位置(10gR2)
http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14261/case_statement.htm#i36967
5.小结
“小CASE,大用途”。在实现简单分支语句功能上面CASE有它自身的优势。
Good luck.
secooler
09.12.15
-- The End --
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70034820/viewspace-2994180/
,如需转载,请注明出处,否则将追究法律责任。