1、Oracel中的decode
DB2解决方案:用case条件表达式完成。
case两种语法模式:
(1)CASE
WHEN 条件 THEN 结果1
ELSE 结果2
END
(2)CASE 表达式1
WHEN 表达式2 THEN 结果1
ELSE 结果2
END
上面的WHEN可以重复多次,就像C中的SWITCH ..CASE的表达.
例如:
SELECT ORDNO,CUSNO,
CASE MONTH(SHIPDATE)
WHEN '01' THEN 'Jan'
WHEN '02' THEN 'Feb'
WHEN '03' THEN 'Mar'
WHEN '04' THEN 'Apr'
WHEN '05' THEN 'May'
WHEN '06' THEN 'Jun'
WHEN '07' THEN 'Jul'
WHEN '08' THEN 'Aug'
WHEN '09' THEN 'Sep'
WHEN '10' THEN 'Oct'
WHEN '11' THEN 'Nov'
WHEN '12' THEN 'Dec'
END
FROM FILE
应用实例:
Oracle SQL:
-------------------------
select decode(t.organtypecode, 'D', t.parent, 'S', t.parent, t.id)
from A_ORGAN t
where t.parent = 35
DB2 SQL:
-------------------------
select case x.organtypecode
when 'D' then
x.parent
when 'S' then
x.parent
else
x.id
end
from a_Organ x
where x.parent = 35;
2、Oracle中的Start with...Connect By递归查询
DB2解决方案:用with公共递归表达式来解决。
DB2解决方案:用case条件表达式完成。
Oracle SQL:
-------------------
select t.id
from a_organ t
start with t.id in (select decode(t.organtypecode,
'D',
t.parent,
'S',
t.parent,
t.id)
from A_ORGAN
where t.id = 35)
connect by t.parent = prior t.id
DB2 SQL:
-------------------------
WITH FKK(id) as
(select o.id from a_organ o
where o.id=35
UNION ALL
select case x.organtypecode
when 'D' then x.parent
when 'S' then x.parent
else x.id
end
from FKK fk, a_organ x
where fk.id=x.parent)
select distinct id from FKK;
3、Oracle中的dual表对应DB2中的SYSIBM.SYSDUMMY1表
DB2解决方案:对应于DB2中的 SYSIBM.SYSDUMMY1表
Oracle SQL:
-------------------------
select 15 as ttt from dual
结果:
ttt
-------
15
DB2 SQL:
-------------------------
select 15 as ttt from SYSIBM.SYSDUMMY1
结果:
ttt
-------
15
4、日期转换问题
DB2解决方案:有相应的函数
Oracle SQL:
-------------------------
select m.*
from dj_mcdj m
where m.mcqc || ' ' like '%$P{QYMC}%'
and m.xzqhdm || ' ' like '%$P{XZQH}%'
and m.hylbdm || ' ' like '%$P{HYLB}%'
and m.blqsrq >= to_date('$P{QSRQ}', 'yyyy-mm-dd')
and m.blqsrq < to_date('$P{JZRQ}', 'yyyy-mm-dd')+1
DB2 SQL:
---------------------
--------------------
--名称:名称库查询
--作者:雷智民
--日期:2006-10-27
--FOR :DB2
--------------------
select m.*
from dj_mcdj m
where m.mcqc || ' ' like '%%'
and m.xzqhdm || ' ' like '%%%'
and m.hylbdm || ' ' like '%%%'
and date(m.blqsrq) >= date('1900-01-01')
and date(m.blqsrq) < date('2050-01-01')+1 day
(责任编辑:铭铭 mingming_ky@126.com TEL:(010)68476636)