怎么写动态游标

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

原文地址:https://www.cnblogs.com/qzfitsoft/p/2626876.html