SYBASE 資料庫遷移到AS 400 db2的FAQ(四) (轉)

amyz發表於2007-08-16
SYBASE 資料庫遷移到AS 400 db2的FAQ(四) (轉)[@more@] 

.2的迴圈控制語句l用法?

答:loop例子::namespace prefix = o ns = "urn:schemas--com::office" />

OPEN c1;

SET at_end = 0;

SET numrec = 0;

fetch_loop: 1

LOOP

FETCH c1 INTO proc_cusnbr, proc_cuscrd;

IF CODE = 0 THEN

SET proc_cuscrd = proc_cuscrd * 1.2;

UPDATE ordapplib.customer

SET cuscrd = proc_cuscrd

WHERE CURRENT OF c1;

SET numrec = numrec + 1;

ELSE

LEAVE fetch_loop; 2

END IF;

END LOOP fetch_loop; 3

CLOSE c1;

 

.的迴圈控制語句while用法?

答:while  例子:

OPEN c1;

SET at_end = 0;

SET numrec = 0;

WHILE at_end = 0 DO

FETCH c1 INTO proc_cusnbr, proc_cuscrd;

IF SQLCODE = 0 THEN

SET proc_cuscrd = proc_cuscrd * 1.2;

UPDATE ordapplib.customer

SET cuscrd = proc_cuscrd

WHERE CURRENT OF c1;

SET numrec = numrec + 1;

ELSE

SET at_end = 1;

END IF;

END WHILE;

CLOSE c1;

 

.Db2的迴圈控制語句repeat用法?

答:repeat例子

SET numrec = 0;

fetch_loop:

REPEAT

FETCH c1 INTO proc_cusnbr, proc_cuscrd;

IF SQLCODE = 0 THEN

SET proc_cuscrd = proc_cuscrd * 1.2;

UPDATE ordapplib.customer

SET cuscrd = proc_cuscrd

WHERE CURRENT OF c1;

SET numrec = numrec + 1;

END IF;

UNTIL SQLCODE <> 0

END REPEAT fetch_loop;

 

 

.Db2的迴圈控制語句for用法?

答:for 例子

FOR each_record AS

cursor1 CURSOR FOR

cusnbr, cuscrd FROM ordapplib.customer

DO

UPDATE ordapplib.customer

SET cuscrd = cuscrd * 1.1

WHERE CURRENT OF cursor1;

END FOR;

. 中迴圈控制Break,Continue在Db2用法?

答:在db2中 Break 轉換為leave lab, Continue 轉換為ITERATE lab

  等同於GOTO語句

舉例如下:

==============  leave 例子

OPEN c1;

SET at_end = 0;

SET numrec = 0;

fetch_loop: 1

LOOP

FETCH c1 INTO proc_cusnbr, proc_cuscrd;

IF SQLCODE = 0 THEN

SET proc_cuscrd = proc_cuscrd * 1.2;

UPDATE ordapplib.customer

SET cuscrd = proc_cuscrd

WHERE CURRENT OF c1;

SET numrec = numrec + 1;

ELSE

LEAVE fetch_loop; 2

END IF;

END LOOP fetch_loop;

CLOSE c1;

============  iterate 例子

BEGIN

OPEN c1;

ins_loop: 1

LOOP

FETCH c1 INTO v_dept, v_deptname, v_admdept;

IF at_end = 1 THEN

LEAVE ins_loop;

ELSEIF v_dept = 'D11' THEN

ITERATE ins_loop; 2

END IF;

INSERT INTO sampledb02.deptnew (deptno, deptname, admrdept)

VALUES (v_dept, v_deptname, v_admdept);

END LOOP;

CLOSE c1;

END;

.在Db2過程中使用滾動遊標(scrollable cursor)?

答:例子

CREATE PROCEDURE MYMAX

( IN fld_name CHAR(30),

IN file_name CHAR(128),

INOUT max_value INTEGER)

LANGUAGE SQL

BEGIN atomic

DECLARE sql_stmt CHAR(256);

DECLARE not_found

CONDITION FOR '02000';

DECLARE c1 DYNAMIC SCROLL CURSOR FOR s1;  -- 宣告動態滾動遊標

DECLARE CONTINUE HANDLER FOR not_found

SET max_value = NULL;

SET sql_stmt = 'SELECT ' || fld_name || ' FROM ' || file_name ||

' ORDER BY 1';  --組合sql語句

PREPARE s1 FROM sql_stmt;

OPEN c1;

FETCH LAST FROM c1 INTO max_value;  --轉到最後行

CLOSE c1;

END

滾動遊標的使用例子:(rpg)

EXEC SQL BEGIN DECLARE SECTION;

char fld_name[ 30 ];

char file_name[ 128 ];

integer max_value;

short ind3;

EXEC SQL END DECLARE SECTION;

Then the indicator variable is used in the call statement:

EXEC SQL

CALL MYMAX( :fld_name, :file_name, :max_value :ind3);

 

.db2中儲存過程中使用動態遊標(dynamic cursor)?

答:使用PREPARE , EXECUTE ,EXECUTE IMMEDIATE語句

  例子:

CREATE PROCEDURE DYNSQLSAMPLE()

LANGUAGE SQL

BEGIN

DECLARE stmt VARCHAR(256);

SET stmt = 'UPDATE employee SET salary = salary * 1.1 WHERE empno = ?'; 1

PREPARE s1 FROM stmt;

ins_loop:

FOR each_department AS

c1 CURSOR FOR

SELECT mgrno FROM department WHERE mgrno IS NOT NULL

DO

EXECUTE s1 USING mgrno;

END FOR;

END;

EXECUTE IMMEDIATE statement  例子:

PREPARE s1 FROM ‘UPDATE employee SET salary = salary * 1.1 WHERE

empno IN (SELECT DISTINCT mgrno FROM department WHERE mgrno IS NOT NULL);

EXECUTE s1;

等同於

EXECUTE IMMEDIATE ‘UPDATE employee SET salary = salary * 1.1 WHERE

empno IN (SELECT DISTINCT mgrno FROM department WHERE mgrno IS NOT NULL);

 

最基本動態遊標語句

...

DECLARE stmt VARCHAR[256];

...

SET stmt = ‘SELECT COLUMN1, COLUMN2, COLUMN3 FROM TBL1’;

PREPARE PreparedStatement FROM s1;

DECLARE Cursor1 CURSOR FOR PreparedStatement;

...

.Db2下支援返回結果集合的儲存過程嗎?

答:支援,例子1

CREATE PROCEDURE GetCusName()

RESULT SETS  1

LANGUAGE SQL

BEGIN

DECLARE c1 CURSOR WITH RETURN FOR

SELECT cusnam FROM customer ORDER BY cusnam;

OPEN c1;

SET RESULT SETS CURSOR c1;

END

  例子2

CREATE PROCEDURE GETRANKV4R5

(IN proc_year DECIMAL(4,0),

IN proc_month DECIMAL(2,0),

INOUT proc_rank INTEGER)

RESULT SETS 2  ---- 2 兩個結果集

LANGUAGE SQL

BEGIN

...

DECLARE c1 DYNAMIC SCROLL CURSOR FOR s1;

DECLARE c2 DYNAMIC SCROLL CURSOR FOR s2;

...

SET RESULT SETS CURSOR c1, CURSOR c2;

END

 

.DB2中一個表的行長度、列數以及每頁行數在表空間中的限制

答:在DB2資料庫中一個表的每行長度、列數以及每頁行數在表空間中的限制如下:
[平臺] 9x/NT/2000, ,
[版本] 6.x/7.x
表空間頁面大小 表空間中行長度限制(bytes)表空間中列數限制 表空間中每頁最大行數
4K 4005 500 255

8K 8101 1012 255

16K 16293 1012 255

32K 32677 1012 255

注:表空間頁面大小隻有4K,8K,16K,32K四種。

 

多個表或觸發許多,在對這樣的SQL語句進行編譯時,出現SQL0101N錯誤,如何處理

答:對於一個複雜的SQL語句,在呼叫多個表或觸發多個觸發器時, 可能會佔用
大量的資源.
當出現SQL0101N錯誤時, 首先需要確認系統中沒有遞迴的觸發器存在.
之後可透過增加如下引數的值來解決此問題:

1)STMTHEAP
2)APPLHEAPSZ
3)PCKCACHESZ

答:資料庫建立時日誌方式預設是迴圈日誌(Circular Log),這時是無法做聯機備份的。所以,希望實施聯機備份,首先要將日誌方式改為歸檔日誌模式(Archival Log)。

以sample資料庫為例,可以在控制中心中改變sample資料庫的引數LOGRETAIN為Recovery,或在命令列下用 db2 update db cfg for sample using LOGRETAIN on。改變此引數後,再次連線資料庫會顯示資料庫處於備份暫掛(BACKUP PENDING)狀態。這時,需要做一次對資料庫的離線備份。在控制中心中選擇對資料庫進行離線備份或在命令列下用 db2 backup db sample 實施。此後資料庫就可以進行聯機備份了。
可以選擇在控制中心中對資料庫進行聯機備份,或在命令列下用 db2 backup db sample online 實施。
注意: 利用聯機備份得到的IMAGE進行恢復時,還需要相關的日誌檔案。

qq:50839655

to::dekker@.com">msn:dekker@hotmail.com

:zhangtao@xhzq.com,dekker99@163.com">email:zhangtao@xhzq.com,dekker99@163.com


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752019/viewspace-962557/,如需轉載,請註明出處,否則將追究法律責任。

相關文章