2014-06-25 39 views
-2

我不允許使用遊標或臨時表。我只能使用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 
+1

表變量呢? – Paolo

回答

-1
Select id,name,row_number() over (order by id) 'Row' into #temp 
from table 

Select @id = id from #temp where row = @current 

使用臨時表來獲取下一個ID

+0

這是行不通的... – Azar

+0

他說他不想使用臨時表或遊標:D –

0

使用

SELECT TOP 1 * FROM table WHERE id > @iterator ORDER BY id ASC 
模式

在一個循環

0

如果ID是序列號(M01,M02,M03,...),那麼您可以在使用下列與while循環

SELECT TOP 1 @id = id FROM table WHERE CAST(REPLACE(id,'M','') AS INT) = @CurrRecord 

完整的查詢

DECLARE @TotRecord INT, @CurrRecord INT, @id VARCHAR(10) 

SELECT @TotRecord = COUNT(*) FROM Table1 
SET @CurrRecord = 1 
WHILE @CurrRecord <= @TotRecord 
BEGIN 
    -- Get ID 
    SELECT TOP 1 @id = id FROM Table1 WHERE CAST(REPLACE(id,'M','') AS INT) = @CurrRecord 

    -- Do something 

    SET @CurrRecord = @CurrRecord + 1 
END 
0

您可以使用光標一對一地獲取您的查詢記錄。

例如:

Declare @Col1 INT, 
     @Col2 INT, 
     ... 


DECLARE X CURSOR FOR 
SELECT Col1, col2 , ... 
FROM YourTableName 


OPEN X 
FETCH NEXT FROM X INTO @Col1, @Col2, ... 
WHILE @@FETCH_STATUS=0 BEGIN 
    --your code to do with columns data of your record 
    FETCH NEXT FROM X INTO @Col1, @Col2, ... 
END 
CLOSE X 
DEALLOCATE X 

您還可以使用其中對於你的想法:

DECLARE @Id INT, 
     @OldId INT, 
     @Col1 INT, 
     @Col2 INT 
     ... 

WHILE 1=1 BEGIN 

    SET @OldId = @Id 
    SET @Id = NULL 

    SELECT TOP 1 @Id = Id, 
       @Col1 = Column1, 
       @Col2 = Column2 
    FROM YourTable 
    WHERE (@OldId IS NULL OR Id > @OldId) ORDER BY Id 

    IF @Id IS NULL BREAK 

    -- your Code Here 
END 
0
DECLARE @TotRecord INT; 
DECLARE @CurrRecord INT; 
DECLARE @id VARCHAR(10); 

SELECT @TotRecord = COUNT(*) from Table1; 
SELECT @CurrRecord = 1; 
SELECT TOP 1 @id = id FROM Table1 ORDER BY id; 
WHILE (@CurrRecord <= @TotRecord) 
BEGIN 
    --Do something... 
    --Get the next id 
    SELECT TOP 1 @id = id FROM Table1 WHERE id > @id ORDER BY id; 
    SELECT @CurrRecord = @CurrRecord + 1; 
END; 

但要注意的是,如果該ID是在表1的獨特,這隻會工作。

+0

'id'是一個VARCHAR字段 – Jesuraja

+0

@Jesuraja肯定有一些創意解析和鑄造會做這項工作 – Akash

+0

即使id是一個VARCHAR字段,只要它是唯一的,它並不重要。是的,您不會按照您預期的順序獲取數據,但您將只處理一行一次的每一行......如果訂單很重要,那麼您可以先通過刪除「M」來解析它。 –

0
Declare @TotRecord int, @CurrRecord Int, @id varchar(10) 

    Select @TotRec = COUNT(*) from Table1 
    Set @CurrRec = 1 
    WHILE (@CurrRec <[email protected]) 
    BEGIN 

    select LAST(id) from (SELECT TOP @CurrRec id 
    FROM Table1); 

    //Here you will get the last record, i havent tried this, but iam sure about the logic , hope this one helps 

    SET @CurrRec = @CurrRec + 1 
    END