第一版
2、Where条件弱类型判断
oracle: where 字符型字段 in (整形) 是允许,DB2不允许
select 'abc' from dual where '1' in (1) 在oracle下可通过
select 'abc' from sysibm.sysdummy1 where '1' in (1) 在DB2下报错
oracle:where 字符型字段=数字型字段 允许,DB2不允许
select 'abc' from dual where '1'=1 在oracle下可通过
select 'abc' from sysibm.sysdummy1 whre '1'=1 在DB2下报错
3、replace关键字
oracle支持,DB2不支持 create or replace语句在DB2下是非法的
4、子查询别名
ORACLE 支持select * from(select 1 from dual) 或者 select * from(select 1 from dual) t
DB2 支持select * from(select 1 from sysibm.sysdummy1) t 或者 select * from(select 1 from sysibm.sysdummy1) as t
固兼容的写法是select * from(子查询) t
5、DATE数据类型的区别
ORACLE中DATE型也是带有时分秒的,但DB2下DATE只是年月日,如'2007-04-28',且可作为字符串直接操作,DB2中要记录时分秒必须采用TIMESTAMP型
一个采用hibernate后常见的兼容问题是:
如果在映射文件中定义了某个字段为Date型
则在DB2下,此字段必须定义为timestamp,而不能定义成DATE,不然会报出字符串右截断的错误
对于DB2来说,在查询条件中可以直接用字符串指定日期或时间戳类型字段的值,例如 where create_date = '2007-04-26' 、where create_timestamp = '2007-04-26 08:08:08' ,无须使用字符串转日期函数
6、分页的处理
如果采用JDBC分页的话,注意rownum在DB2中不受支持,比如从masa_area表中取得area_id最小的10条记录,语句分别如下,注意这里的别名t书写方法
ORACLE: select t.* from (select rownum as r1 ,masa_area.* from masa_area order by area_id) t where t.r1<=10
DB2: select t.* from (select rownumber() over() as r1 ,masa_area.* from masa_area order by area_id) t where t.r1<=10
第二版
1、取前N条记录
Oracle:Select * from TableName where rownum <= N;
DB2:Select * from TableName fetch first N rows only;
2、取得系统日期
Oracle:Select sysdate from dual;
DB2:Select current timestamp from sysibm.sysdummy1;
3、空值转换
Oracle:Select productid,loginname,nvl(cur_rate,'0') from TableName ;
DB2:Select productid,loginname,value(cur_rate,'0') from TableName;
Coalesce(cur_rate,'0')
4、类型转换(8版有了to_char,to_date,9版新增了to_number)
Oracle:select to_char(sysdate,'YYYY-MM-DD HH24:MI:SS') from dual;
DB2:select varchar(current timestamp) from sysibm.sysdummy1;
l Oracle数据类型改变函数:to_char()、to_date()、to_number()等;如果仅仅取年,月,日等,可以用to_char(sysdate, 'YYYY'),to_char('MM') ,to_char('DD')取得。只取年月日TRUNC(SYSDATE),取时分秒TO_CHAR(SYSDATE,'HH24:MI:SS')。
l DB2数据类型改变函数:char()、varchar()、int()、date()、time()等;取得年,月,日等的写法:YEAR(current timestamp),MONTH(current timestamp),DAY(current timestamp),HOUR(current timestamp),MINUTE(current timestamp),SECOND(current timestamp),MICROSECOND(current timestamp),只取年月日可以用DATE(current timestamp),取时分秒TIME(current timestamp)。Char()是定长字符串(1-255),varchar()为非定长字符串(1-32672)日期,时间形态变为字符形态: char(current date),char(current time)将字符串转换成日期或时间形态:TIMESTAMP('2002-10-2012:00:00'),DATE('2002-10-20'),DATE('10/20/2002'),TIME('12:00:00')
l 目前DB2 V8也支持to_char和to_date
5、快速清空大表
Oracle:truncate table TableName ;
DB2:alter table TableName active not logged initially with empty table;
6、To_Number
Oracle:select to_number('123') from dual;
DB2:select cast('123' as integer) from sysibm.sysdummy1;
SELECT CAST ( current time as char(8)) FROMsysibm.sysdummy1
7、创建类似表
Oracle:create table a as select * from b ;
DB2:create table a like b ;
CREATE TABLE tab_newAS select col1,col2…FROMtab_old DEFINITION ONLY (8版有效,9版无效)
8、decode方法
Oracle:decode方法(DECODE(条件,值1,翻译值1,值2,翻译值2,...值n,翻译值n,缺省值))或者case语句DB2中只有CASE表达式SELECT id ,name ,CASE WHEN integer(flag)=0 THEN ‘假’ WHEN integer(flag)=1 THEN ‘真’ ELSE ‘异常’END FROM TEST或者SELECT id ,name , CASE integer(flag) WHEN 0 THEN ‘假’ WHEN 1 THEN ‘真’ELSE ‘异常’END FROM TEST
9、数据类型
比较大的差别:
Oracle:char 2000
DB2: char 254
Oracle: date datetime
Db2: DATE:日期TIME:时间TIMESTAMP:日期时间