DB2編程的正確應(yīng)用程序
以下的文章主要描述的是DB2編程,我們首先是從建存儲(chǔ)過程時(shí)Create 后一定不要用TAB鍵開始對(duì)其進(jìn)行講述的,如果你對(duì)DB2編程,心存好奇的話,以下的文章將會(huì)揭開它的神秘面紗。
1.1 建存儲(chǔ)過程時(shí)Create 后一定不要用TAB鍵
- create procedure
的create后只能用空格,而不可用tab健,否則編譯會(huì)通不過。
切記,切記。
1.2 使用臨時(shí)表
要注意,臨時(shí)表只能建在user tempory tables space 上,如果database只有system tempory table space是不能建臨時(shí)表的。
另外,DB2的臨時(shí)表和sybase及oracle的臨時(shí)表不太一樣,DB2的臨時(shí)表是在一個(gè)session內(nèi)有效的。所以,如果程序有多線程,***不要用臨時(shí)表,很難控制。
建臨時(shí)表時(shí)***加上 with replace選項(xiàng),這樣就可以不顯示的drop 臨時(shí)表,建臨時(shí)表時(shí)如果不加該選項(xiàng)而該臨時(shí)表在該session內(nèi)已創(chuàng)建且沒有drop,這時(shí)會(huì)發(fā)生錯(cuò)誤。
1.3 從數(shù)據(jù)表中取指定前幾條記錄
- select * from tb_market_code fetch first 1 rows only
但下面這種方式不允許
- select market_code into v_market_code
- from tb_market_code fetch first 1 rows only;
選***條記錄的字段到一個(gè)變量以以下方式代替
- declare v_market_code char(1);
- declare cursor1 cursor for select market_code from tb_market_code
- fetch first 1 rows only for update;
- open cursor1;
- fetch cursor1 into v_market_code;
- close cursor1;
1.4 游標(biāo)的使用
注意commit和rollback
使用游標(biāo)時(shí)要特別注意如果沒有加with hold 選項(xiàng),在Commit和Rollback時(shí),該游標(biāo)將被關(guān)閉。Commit 和Rollback有很多東西要注意。特別小心
游標(biāo)的兩種定義方式
一種為
- declare continue handler for not found
- begin
- set v_notfound = 1;
- end;
- declare cursor1 cursor with hold for select market_code from tb_market_code for update;
- open cursor1;
- set v_notfound=0;
- fetch cursor1 into v_market_code;
- while v_notfound=0 Do
- --work
- set v_notfound=0;
- fetch cursor1 into v_market_code;
- end while;
- close cursor1;
這種方式使用起來比較復(fù)雜,但也比較靈活。特別是可以使用with hold 選項(xiàng)。如果循環(huán)內(nèi)有commit或rollback 而要保持該cursor不被關(guān)閉,只能使用這種方式以上的相關(guān)內(nèi)容就是對(duì)DB2編程序技巧部分內(nèi)容的介紹,望你能有所收獲。
【編輯推薦】