Chinaunix首页 | 论坛 | 博客
  • 博客访问: 334243
  • 博文数量: 329
  • 博客积分: 2633
  • 博客等级: 少校
  • 技术积分: 3633
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-22 15:43
文章分类

全部博文(329)

文章存档

2013年(244)

2012年(46)

2011年(39)

我的朋友

分类: 数据库开发技术

2011-08-22 19:22:00

一般的使用就是一个循环,游标是取到所在条件的一行行,依次进行操作。
例如:
 
  1. declare cur_subjectDetail cursor for --定义一个游标


  2.     select UUID
  3.     from TB_SUBJECTDETAIL

  4.     where Tb_Subject_UUID=@UUID

  5.     --打开并调用游标


  6.     open cur_subjectDetail

  7.     fetch next from cur_subjectDetail

  8.     into @SUBJECTDETAIL_UUID
  9.     --循环


  10.     while (@@FETCH_STATUS=0)--状态等于0说明fetch正常


  11.     begin

  12.         --处理,自己想要的语句--

  13.         --处理,自己想要的语句--



  14.         fetch next from cur_subjectDetail

  15.         into @SUBJECTDETAIL_UUID--取下一个

  16.     end

  17.     --关闭

  18.     close cur_subjectDetail
  19.     --删除游标

  20.     deallocate cur_subjectDetail
 
当然也可以是游标套游标的,实现类似于两重循环的效果。
例如:
    declare cur_subject cursor for
    select SENDTEL_FLAG
    from TB_SUBJECT
    --打开并调用游标
    open cur_subject
    fetch next from cur_subject
    into @SENDTEL_FLAG
    --循环
    while (@@FETCH_STATUS=0)
    begin 
  1. declare cur_subjectDetail cursor for --定义一个游标


  2.     select UUID
  3.     from TB_SUBJECTDETAIL

  4.     where Tb_Subject_UUID=@UUID

  5.     --打开并调用游标


  6.     open cur_subjectDetail

  7.     fetch next from cur_subjectDetail

  8.     into @SUBJECTDETAIL_UUID
  9.     --循环


  10.     while (@@FETCH_STATUS=0)--状态等于0说明fetch正常


  11.     begin

  12.         --处理,自己想要的语句--

  13.         --处理,自己想要的语句--



  14.         fetch next from cur_subjectDetail

  15.         into @SUBJECTDETAIL_UUID--取下一个

  16.     end

  17.     --关闭


  18.     close cur_subjectDetail

  19.     --删除游标


  20.     deallocate cur_subjectDetail

 

 

 fetch next from cur_subject
 into @SENDTEL_FLAG

end

--关闭
close cur_subject
--删除游标
deallocate cur_subject

阅读(638) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~