动态游标


舉個例子,你再根據你的具體情況進行修改:

declare @sqlExec varchar(8000),@tableName varchar(255)
set @tableName = 'sysobjects'
set @sqlExec = 'declare cursor1 cursor for ' + CHAR(13)
set @sqlExec = @sqlExec + ' select * from ' + @tableName
exec(@sqlExec)
open cursor1
fetch next from cursor1 ---如果要放在变量中请写into <变量列表>
while @@fetch_status = 0
......
fetch next from cursor1 ---如果要放在变量中请写into <变量
end
close cursor1
deallocate cursor1

Published At
Categories with 数据库类
Tagged with
comments powered by Disqus