use abc
godeclare @two_location varchar(50)
declare two_cur cursor for
select two_location
from two_usersopen two_curfetch next from two_cur into @two_location
while (@@fetch_status=0)
  begin
    --我想在这里,对查询出的@two_location再执行一个while的游标操作,可以么?
    fetch next from two_cur into @two_location
  endclose two_cur
deallocate two_cur

解决方案 »

  1.   

    declare 外层游标
    open 外层游标
    fetch next ...提取外层游标行
    while @@FETCH_STATUS = 0
    begin
        declare 内层游标
        open 内层游标
        ftech next ...提取内层游标行
        while @@FETCH_STATUS = 0
        begin
              .....处理内层游标
              ftech next ....内层游标向下移动一行
        end
        close 内层游标
        deallocate 内层游标
        fetch next ....内层游标处理结束后,外层游标才继续向下移动一行
    end 
    close 外层游标
    deallocate 外层游标
      

  2.   

    可以,给段参考~   
      declare   my_cur   cursor   for     
      select   ...   open   my_cur   
      fetch   next   from   my_cur   into   @tablestring   
      while   @@FETCH_STATUS   =   0   
      begin   
                                  ...   
      declare   columns_cur   cursor   for   
      select   ...   
      open   columns_cur   
      fetch   next   from   columns_cur   into   @columnstring   
      while   @@FETCH_STATUS   =   0   
      begin   
      ... fetch   next   from   columns_cur   into   @columnstring   
      end   
      close   columns_cur   
      DEALLOCATE   columns_cur   
      close   my_cur   
      DEALLOCATE   my_cur