草庵书生

冷眼面对一切,低调!才是最牛B的炫耀!
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

怎么写动态游标

Posted on 2012-08-07 16:07  草庵书生  阅读(162)  评论(0编辑  收藏  举报
create table tb(id int)
insert tb select 1 union select 2
go

declare @tb nvarchar(100)
set @tb='tb'
exec('declare cur cursor for select id from '+@tb+' where id<10')
open cur
declare @id int
fetch next from cur into @id
while @@fetch_status=0
begin
   
print @id
   
fetch next from cur into @id
end
deallocate cur
go