declare @column1 varchar(50)=''
declare @column2 varchar(50)=''
declare @column3 varchar(50)=''
declare @cursonExist int=0
declare cursorName cursor for
select column1,column2,column3 from table where 1 = 1 order by column1
open cursorName--打开游标
fetch next from cursorName into @column1,@column2,@column3
while @@fetch_status = 0
begin
--循环遍历每一条记录,这里写业务逻辑
fetch next from cursorName into @column1,@column2,@column3
end
close cursorName
deallocate cursorName
--删除使用的游标
select @cursonExist=count(1) from MASTER.dbo.syscursors where cursor_name='cursorName'
if (@cursonExist>0) deallocate cursorName