oracle里的case功能非常强大,
简单的用法:
select
CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他' ENDfrom student;
还没有当做预处理去使用
select (case 1
when 1 then 11
when 2 then 22
else 33 end) from dual;
绑定变量
SQL>variable vcc varchar2(10); SQL> exec :vcc := 'test'; SQL> select :vcc from dual;
定义一个变量 :type,就可以和case混合使用了
select (case :type
when 1 then 11
when 2 then 22
else 33 end) from dual;
--
使用示例
select
to_char(oper_time, (case :type
when 1 then 'yyyymmdd'
when 2 then 'yyyymm'
when 3 then 'yyyy'
else 'yyyy' end)) oper_time, count(1)
from info group by to_char(oper_time, (case :type
when 1 then 'yyyymmdd'
when 2 then 'yyyymm'
when 3 then 'yyyy'
else 'yyyy' end));
根据绑定变量的值的变化去动态执行相应的语句
在PL/SQL Developer里,sql窗口中,选中sql语句,右键->测试, 填上绑定变量的值,就可以方便的查看绑定变量的使用情况,
可以通过测试发现某个位置是否可以使用绑定变量