自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

DB2存儲過程中的兩種指針循環(huán)方式

數據庫
DB2數據中的存儲過程有兩種指針循環(huán),分別是for循環(huán)和while循環(huán)。for循環(huán)比while循環(huán)簡單很多,但是,有時候必須使用while循環(huán)。

DB2存儲過程相信大家都比較了解了,下面為您介紹的是DB2存儲過程中的兩種指針循環(huán)方式,希望對您學習DB2存儲過程能有所幫助。

DB2存儲過程中的指針循環(huán):

DB2存儲過程有2種那個方式:for循環(huán)和while循環(huán),如:

for循環(huán):

  1. for c1 as select deliveryid,deliverycode from delivery where status=40 for read only do  
  2.  
  3. select sum(qty) into dQty from deliverydetail where deliveryid=c1.deliveryid;  
  4.  
  5. ......  
  6.  
  7. end for;  

while循環(huán):

  1. declare c1 cursor for select deliveryid,deliverycode from delivery where status=40 for read only ;  
  2.  
  3. DECLARE CONTINUE HANDLER FOR NOT FOUND SET at_end_c1 = 1;  
  4.  
  5. set at_end_c1=0;  
  6. open c1 ;  
  7. FETCH c1 INTO ndeliveryid ,sdeliverycode;  
  8. WHILE ( at_end_c1 = 0) do  
  9.      select sum(qty) into dQty from deliverydetail where deliveryid=ndeliveryid ;  
  10.  
  11. ...  
  12.  
  13. set at_end_c1=0;  
  14.     FETCH c1 INTO ndeliveryid ,sdeliverycode;  
  15. END while ;  
  16. close c1;  

很明顯,for循環(huán)比while循環(huán)簡單很多

而且,對于while循環(huán),如果不注意,很容易出現(xiàn)死循環(huán)

一般建議使用for循環(huán).

但是,有時候必須使用while循環(huán)。

如果在循環(huán)中有事務,比如在循環(huán)中有一塊要求獨立事務,對于那一塊,如果執(zhí)行成功 ,就提交這一塊,如果執(zhí)行不成功,就回滾這一塊,繼續(xù)下一個循環(huán),之時候就只能用while循環(huán)。

因為,當commit的時候,會自動關閉當前指針。

在while循環(huán)中,如果要commit而不關閉當前指針,要在定義指針的時候加with hold,上面的指針定義為:

  1. declare c1 cursor with hold for select deliveryid,deliverycode from delivery where status=40 for read only ; 

對于for循環(huán)是沒有辦法處理的。

如果需要用rollback,需要定義savepoint,然后回滾到指定的回滾點。
 

 

 

 

【編輯推薦】

DB2創(chuàng)建數據庫的實現(xiàn)

DB2目錄視圖說明

DB2數據庫命令大全

DB2裝入命令的性能因素

DB2列轉行的實現(xiàn)

責任編輯:段燃 來源: 互聯(lián)網
相關推薦

2010-08-31 17:14:24

DB2表空間

2010-11-04 10:16:38

db2日志保存

2010-08-26 15:15:18

DB2備份

2010-08-31 15:39:25

DB2存儲過程

2010-08-05 14:24:37

DB2存儲過程

2010-11-02 10:27:38

DB2分區(qū)數據庫恢復

2010-09-07 09:43:34

DB2提供

2010-11-01 09:54:24

DB2管理服務器

2010-11-03 10:35:45

DB2存儲過程

2010-07-27 13:46:08

DB2提高IMPORT

2010-08-02 13:05:01

DB2應用

2010-08-10 13:36:00

2010-08-18 13:29:11

DB2存儲過程

2010-11-04 13:58:58

DB2存儲過程

2010-11-03 10:26:22

DB2存儲過程

2010-11-03 10:46:49

DB2存儲過程

2018-02-01 08:25:10

DB2存儲方法

2010-08-10 15:30:21

2010-08-05 10:42:33

DB2 拉鏈表存儲過程

2010-11-04 09:31:21

DB2循環(huán)語句
點贊
收藏

51CTO技術棧公眾號