205

How do I loop through a set of records from a select statement?

Say I have a few records that I wish to loop through and do something with each record. Here's a primitive version of my select statement:

select top 1000 * from dbo.table
where StatusID = 7
10
  • 5
    What do you want to do to each record? The preference would be to do the work in a SQL query. Barring that you would need to use T-SQL, perhaps with cursors. Dec 18, 2013 at 15:42
  • 2
    I would use a Cursor.
    – FloChanz
    Dec 18, 2013 at 15:42
  • 5
    That will be quite slow - is it not possible to re-write the stored proc or move some of the logic out of it to work in a set-based manner?
    – Bridge
    Dec 18, 2013 at 15:45
  • 2
    @Funky what does the sproc do? Often code can be re-written in a set based manner (i.e. avoid loops). If you're adamant you want to perform an RBAR operation (simple-talk.com/sql/t-sql-programming/…) then a cursor is the thing you want to investigate.
    – gvee
    Dec 18, 2013 at 15:50
  • 1
    Perhaps you can explain what you will be doing with this data in more detail. In most cases you can easily write a single SQL query that will do what you need to get done in one action instead of looping through individual records. Dec 18, 2013 at 15:52

8 Answers 8

274

By using T-SQL and cursors like this :

DECLARE @MyCursor CURSOR;
DECLARE @MyField YourFieldDataType;
BEGIN
    SET @MyCursor = CURSOR FOR
    select top 1000 YourField from dbo.table
        where StatusID = 7      

    OPEN @MyCursor 
    FETCH NEXT FROM @MyCursor 
    INTO @MyField

    WHILE @@FETCH_STATUS = 0
    BEGIN
      /*
         YOUR ALGORITHM GOES HERE   
      */
      FETCH NEXT FROM @MyCursor 
      INTO @MyField 
    END; 

    CLOSE @MyCursor ;
    DEALLOCATE @MyCursor;
END;
18
  • 5
    The right thing is to rewrite teh process so that it does not need to loop. Looping is an extremely bad choice in a database.
    – HLGEM
    Dec 18, 2013 at 15:54
  • 35
    Perhaps you're right but with the information given in the question at the time I wrote the answer the user just want to loop through a set of data...and a Cursor is a way to do it.
    – FloChanz
    Dec 18, 2013 at 16:02
  • 25
    Cursors are just a tool -- nothing generally right or wrong about them. Observe the performance and decide. This answer (cursors) is one possible choice. You can also use a WHILE LOOP, CTE, etc.
    – Chains
    Dec 18, 2013 at 16:09
  • 2
    @FrenkyB Yes you can. Look this way... stackoverflow.com/questions/11035187/…
    – sam yi
    Jul 14, 2015 at 15:55
  • 2
    Congratulations, your solution is even on msdn: msdn.microsoft.com/en-us/library/… and I really like how you use the Field Data Type.
    – Pete
    Sep 15, 2016 at 22:47
148

This is what I've been doing if you need to do something iterative... but it would be wise to look for set operations first. Also, do not do this because you don't want to learn cursors.

select top 1000 TableID
into #ControlTable 
from dbo.table
where StatusID = 7

declare @TableID int

while exists (select * from #ControlTable)
begin

    select top 1 @TableID = TableID
    from #ControlTable
    order by TableID asc

    -- Do something with your TableID

    delete #ControlTable
    where TableID = @TableID

end

drop table #ControlTable
4
  • Thank you! My Example with update and group by logic using above code : pastebin.com/GAjUNNi9. Maybe will be useful to anybody.
    – Nigrimmist
    Jan 11, 2016 at 15:27
  • can the variable be used as a column name in update statement inside the loop? Something like "Update TableName SET @ColumnName=2"
    – M H
    Jan 20, 2016 at 16:21
  • 3
    This actually answers the original question, as it lets you iterate over the entire row; whereas cursor lets you iterate over a particular column in a row. Apr 26, 2018 at 19:50
  • 2
    Cursors are more verbose than while statements, but they're more optimized. I wouldn't recommend a while statement, especially with a subquery in the loop condition. Apr 16, 2019 at 3:50
45

Small change to sam yi's answer (for better readability):

select top 1000 TableID
into #ControlTable 
from dbo.table
where StatusID = 7

declare @TableID int

while exists (select * from #ControlTable)
begin

    select @TableID = (select top 1 TableID
                       from #ControlTable
                       order by TableID asc)

    -- Do something with your TableID

    delete #ControlTable
    where TableID = @TableID

end

drop table #ControlTable
0
25

By using cursor you can easily iterate through records individually and print records separately or as a single message including all the records.

DECLARE @CustomerID as INT;
declare @msg varchar(max)
DECLARE @BusinessCursor as CURSOR;

SET @BusinessCursor = CURSOR FOR
SELECT CustomerID FROM Customer WHERE CustomerID IN ('3908745','3911122','3911128','3911421')

OPEN @BusinessCursor;
    FETCH NEXT FROM @BusinessCursor INTO @CustomerID;
    WHILE @@FETCH_STATUS = 0
        BEGIN
            SET @msg = '{
              "CustomerID": "'+CONVERT(varchar(10), @CustomerID)+'",
              "Customer": {
                "LastName": "LastName-'+CONVERT(varchar(10), @CustomerID) +'",
                "FirstName": "FirstName-'+CONVERT(varchar(10), @CustomerID)+'",    
              }
            }|'
        print @msg
    FETCH NEXT FROM @BusinessCursor INTO @CustomerID;
END
3
  • 1
    this looks interesting. I wonder what the @ identifier means.
    – netskink
    Oct 24, 2019 at 14:21
  • 1
    @ is just to differentiate as variables. Nov 13, 2019 at 15:44
  • 2
    This is definitely interesting. But please enlighten me: ---> How the 'SET @msg ...' section getting the Data from Customer? And how are the Field identified as LastName or FirstName returning the value? From which variable? I don't understand that part if you may explain please Mar 14, 2021 at 11:32
9

Just another approach if you are fine using temp tables.I have personally tested this and it will not cause any exception (even if temp table does not have any data.)

CREATE TABLE #TempTable
(
    ROWID int identity(1,1) primary key,
    HIERARCHY_ID_TO_UPDATE int,
)

--create some testing data
--INSERT INTO #TempTable VALUES(1)
--INSERT INTO #TempTable VALUES(2)
--INSERT INTO #TempTable VALUES(4)
--INSERT INTO #TempTable VALUES(6)
--INSERT INTO #TempTable VALUES(8)

DECLARE @MAXID INT, @Counter INT

SET @COUNTER = 1
SELECT @MAXID = COUNT(*) FROM #TempTable

WHILE (@COUNTER <= @MAXID)
BEGIN
    --DO THE PROCESSING HERE 
    SELECT @HIERARCHY_ID_TO_UPDATE = PT.HIERARCHY_ID_TO_UPDATE
    FROM #TempTable AS PT
    WHERE ROWID = @COUNTER

    SET @COUNTER = @COUNTER + 1
END


IF (OBJECT_ID('tempdb..#TempTable') IS NOT NULL)
BEGIN
    DROP TABLE #TempTable
END
4
  • This is really weird. It contains a lot of errors, also using of two variables where one goes from 1 to COUNT(*) and second goes from COUNT(*) to 1 is weird. Feb 9, 2016 at 16:50
  • The variable MAXID is used to LOOP through. The variable COUNTER is used to perform an operation on a particular record in the table. If I read the question it talks about " have a few records that I wish to loop through and do something with each record". I may be wrong but please point out what is wrong above @DAWID
    – Sandeep
    Feb 9, 2016 at 22:22
  • 2
    I think it's obvious how you use those variables in your code. You can just have WHILE (@COUTNER <= @ROWID) and you don't need to decrement @ROWID in each iteration. BTW what happens if ROWIDs in your table are not continuous (some rows were previously deleted). Feb 10, 2016 at 10:24
  • 1
    When would you suggest using a Temp Table over using a Cursor? Is this merely a design choice, or does one have better performance?
    – h0r53
    Jun 16, 2016 at 19:22
7

You could choose to rank your data and add a ROW_NUMBER and count down to zero while iterate your dataset.

-- Get your dataset and rank your dataset by adding a new row_number
SELECT  TOP 1000 A.*, ROW_NUMBER() OVER(ORDER BY A.ID DESC) AS ROW
INTO #TEMPTABLE 
FROM DBO.TABLE AS A
WHERE STATUSID = 7;

--Find the highest number to start with
DECLARE @COUNTER INT = (SELECT MAX(ROW) FROM #TEMPTABLE);
DECLARE @ROW INT;

-- Loop true your data until you hit 0
WHILE (@COUNTER != 0)
BEGIN

    SELECT @ROW = ROW
    FROM #TEMPTABLE
    WHERE ROW = @COUNTER
    ORDER BY ROW DESC

    --DO SOMTHING COOL  

    -- SET your counter to -1
    SET @COUNTER = @ROW -1
END

DROP TABLE #TEMPTABLE
1
  • 1
    ORDER BY is useless inside the loop
    – robotik
    Jun 11, 2021 at 14:45
2

this way we can iterate into table data.

DECLARE @_MinJobID INT
DECLARE @_MaxJobID INT
CREATE  TABLE #Temp (JobID INT)

INSERT INTO #Temp SELECT * FROM DBO.STRINGTOTABLE(@JobID,',')
SELECT @_MinJID = MIN(JobID),@_MaxJID = MAX(JobID)  FROM #Temp

    WHILE @_MinJID <= @_MaxJID
    BEGIN

        INSERT INTO Mytable        
        (        
            JobID,        
        )        

        VALUES        
        (        
            @_MinJobID,        
        ) 

        SET @_MinJID = @_MinJID + 1;
    END

DROP TABLE #Temp

STRINGTOTABLE is user define function which will parse comma separated data and return table. thanks

0

I think this is the easy way example to iterate item.

declare @cateid int
select CateID into [#TempTable] from Category where GroupID = 'STOCKLIST'

while (select count(*) from #TempTable) > 0
begin
    select top 1 @cateid = CateID from #TempTable
    print(@cateid)

    --DO SOMETHING HERE

    delete #TempTable where CateID = @cateid
end

drop table #TempTable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.