游标
declare @ID int, @PID int, @Ver int
declare #cur cursor for
select 1 as ID,1 as ParentId,1 as Version
union select 2, 1, 2 union select 3, 1, 3
open #cur
fetch next from #cur into @ID, @PID, @Ver
while @@FETCH_STATUS=0
begin
print('do some thing.')
fetch next from #cur into @ID, @PID, @Ver
end
close #cur
deallocate #cur
declare @ID int, @PID int, @Ver int
declare #cur cursor for
select 1 as ID,1 as ParentId,1 as Version
union select 2, 1, 2 union select 3, 1, 3
open #cur
fetch next from #cur into @ID, @PID, @Ver
while @@FETCH_STATUS=0
begin
print('do some thing.')
fetch next from #cur into @ID, @PID, @Ver
end
close #cur
deallocate #cur
其中fetch next from #cur into @ID, @PID, @Ver是读取当前行的记录,按Column的顺序放到这三个变量中。
等同select @ID=ID,@PID=PID,@Ver=Ver from table where 条件.