lxpeng163
作者lxpeng163·2013-07-22 22:14
项目经理·哈尔滨银行

DB2中游标的使用小结

字数 5144阅读 4745评论 0赞 1


 

DB2

中游标的使用

 

注意

 

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 

选项。如果循环内有

commit

r

ollback 

而要保持该

cursor

不被关闭,只能使用这种方式。

 

 

另一种为

 

   

pcursor1: for loopcs1 as cousor1 cursor as 

select market_code as market_code 

   

from tb_market_code 

   

for update 

   

do 

   

end for; 

   

这种方式的优点是比较简单,不用(也不允许)使用

 

open,fetch,close

 

但不能使用

with hold 

选项。如果在游标循环内要使用

commit,rollback

则不能使用这种方式。如果没有

c

ommit

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, EXCEP

T, or INTERSECT 

 

UNION ALL

除外)一起使用。


 

DB2

中游标的使用

 

注意

 

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 

选项。如果循环内有

commit

r

ollback 

而要保持该

cursor

不被关闭,只能使用这种方式。

 

 

另一种为

 

   

pcursor1: for loopcs1 as cousor1 cursor as 

select market_code as market_code 

   

from tb_market_code 

   

for update 

   

do 

   

end for; 

   

这种方式的优点是比较简单,不用(也不允许)使用

 

open,fetch,close

 

但不能使用

with hold 

选项。如果在游标循环内要使用

commit,rollback

则不能使用这种方式。如果没有

c

ommit

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, EXCEP

T, or INTERSECT 

 

UNION ALL

除外)一起使用。


  DB2中游标的使用  注意 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 选项。如果循环内有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;     这种方式的优点是比较简单,不用(也不允许)使用 open,fetch,close。  但不能使用with hold 选项。如果在游标循环内要使用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除外)一起使用。

如果觉得我的文章对您有用,请点赞。您的支持将鼓励我继续创作!

1

添加新评论0 条评论

Ctrl+Enter 发表

作者其他文章

相关文章

相关问题

相关资料

X社区推广