我不允許使用遊標或臨時表。我只能使用while循環。使用while循環獲取下一條記錄
表1 - 只讀表(不可編輯)
id name
M01 Raja
M02 Ravi
M03 Vijay
M04 suresh
查詢
Declare @TotRecord int, @CurrRecord Int, @id varchar(10)
Select @TotRec = COUNT(*) from Table1
Set @CurrRec = 1
WHILE (@CurrRec <[email protected])
BEGIN
--*Here i want to get the id from table,
--next time i need to get second id.
--next time i need to get third id.
--....
--1st time i can get the top 1 id by using this below query
Select top 1 @id = id from table
--Next time i want to get second id.*
SET @CurrRec = @CurrRec + 1
END
表變量呢? – Paolo