Declare @row int, --行記錄數
@count int,--總記錄數
@id int --你需要的結果
SELECT ROW_NUMBER() OVER (ORDER BY id ASC) rowid,* into #t from 表名
while @row <= @count --循環開始
BEGIN
select @id=id from #t where rowid=@row --當前列的數據
//這里編寫自己的邏輯
set @row=@row +1
END
drop table #t