DB2編程序的技巧之游標的正確使用
我們今天主要向大家講述的是DB2編程序的小技巧之游標的使用,如果你對DB2編程序的小技巧之游標的使用有興趣的話,你就可以對以下的文章點擊觀看了,以下就是文章的主要內(nèi)容的詳細描述,望大家在瀏覽之后會對其有更深的了解。
注意commit和rollback
使用游標時要特別注意如果沒有加with hold 選項,在Commit和Rollback時,該游標將被關閉。Commit 和Rollback有很多東西要注意。特別小心
游標的兩種定義方式
一種為
- 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;
 
這種方式使用起來比較復雜,但也比較靈活。特別是可以使用with hold 選項。如果循環(huán)內(nèi)有commit或rollback 而要保持該cursor不被關閉,只能使用這種方式。
另一種為
- pcursor1: for loopcs1 as cousor1 cursor as
 - select market_code as market_code
 - from tb_market_code
 - for update
 - do
 - end for;
 
這種方式的優(yōu)點是比較簡單,不用(也不允許)使用open,fetch,close。
但不能使用with hold 選項。如果在游標循環(huán)內(nèi)要使用commit,rollback則不能使用這種方式。如果沒有commit或rollback的要求,推薦使用這種方式(看來For這種方式有問題)。
修改游標的當前記錄的方法
- update tb_market_code set market_code='0' where current of cursor1;
 
不過要注意將cursor1定義為可修改的游標
- declare cursor1 cursor for select market_code from tb_market_code
 - for update;
 
for update 不能和GROUP BY、 DISTINCT、 ORDER BY、 FOR READ ONLY及UNION, EXCEPT, or INTERSECT但 UNION ALL除外)一起使用。

上述的相關內(nèi)容就是對DB2編程序的小技巧之游標的使用的描述,希望會給你帶來一些幫助在此方面。
【編輯推薦】















 
 
 
 
 
 
 