创建游标

declare myCursor cursor for

select [name] from [table]

//declare @result int

  declare @final_result int

打开游标

open myCursor

定义游标结果集的单个对象

declare @subname sysname

//declare @sql nvarchar(500)

执行游标-代码

fetch next from myCursor into @subname

while(@@fetch_status=0)    //游标遍历状态:0为成功

  begin

    set @sql='select count(*) from '+@subname'

    exec sp_executesql @sql,N'@a int output',@result output   //sql 获得exec的值

    fetch next from myCursor into @subname

  end

关闭游标

close myCursor

deallocate myCursor

 

 

转载于:https://www.cnblogs.com/ruanyifeng/archive/2012/06/27/2565517.html

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐