1. 程式人生 > >DB2和Oracle的SQL語法對比

DB2和Oracle的SQL語法對比

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;
 
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')。
 
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')
 
目前DB2 V8也支援to_char和to_date
 
5、快速清空大表
 
Oracle:TRUNCATE TABLE TableName ;
 
DB2:ALTER TABLE TableName active NOT logged initially WITH empty TABLE;
 
6、關於ROWID
 
Oracle它是由資料庫唯一產生的,在程式裡可以獲得DB2       v8也有此功能。
 
7、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
 
8、建立類似表
 
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版無效)
 
9、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
 
10、子查詢(8版,9版也支援子查詢)
 
Oracle:直接用子查詢
 
Db2:WITH語句WITH  a1 AS(SELECT max(id) AS aa1 FROM test )   SELECT id ,aa1 FROM test ,a1
 
11、資料型別
 
比較大的差別:
 
Oracle:char  2000
 
DB2:      char  254
 
Oracle:  date   datetime
 
Db2:       DATE:日期TIME:時間TIMESTAMP:日期時間
 
12、遞迴查詢
Oracle語法簡單,CONNECT BY PRIOR ... START WITH ...:
SELECT DISTINCT parent
 FROM (SELECT T.parent FROM TABLE1 T
 CONNECT BY  PRIOR  T.parent = T.child --遞迴的方向為從子向父
 START WITH T.child = 'xxx' )
 
DB2較難理解,要WITH一個虛擬表:
 WITH PAR_CH(child, parent) AS
 ( SELECT child, parent FROMTABLE1
 WHEREchild = 'xxx' --設定遞迴起點
  UNION ALL
 SELECTT.child, T.parent
 FROMPAR_CH VT, TABLE1 T
 WHERE VT.parent = T.child --遞迴的方向為從子向父
 ) 
SELECT DISTINCT parent

 FROM PAR_CH


如上圖,從grandchild開始查詢其所有的父節點,首先設定虛擬表起點,即左邊第四行;依次往上推,其父親是son = 上一行的child,即VT.parent = T.child...

最後查詢結果為:

---------------------------

son

father

grandpa

---------------------------