Solved

Looping through the records

Posted on 2013-01-27
4
268 Views
Last Modified: 2013-01-31
Hello,

I have lots of records in my table and I want to perform looping on the table. Using CURSORS would is the option I know. Is there any other way to perform looping?? CURSORS have performance issues, right?

Please help!!

Thanks in advance
0
Comment
Question by:ravichand-sql
4 Comments
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 38825380
What is it that you are really trying to do?  "Looping on the table" does not tell us what you really need: are you doing an update?  A select?  Something else?

In any event, cursors should be avoided if what you need to do can be accomplished in a set-based operation via standard SQL statements.
0
 
LVL 18

Expert Comment

by:Jerry Miller
ID: 38825389
You can try a While loop, but many times looping is not necessary. If you post exactly what you are trying to accomplish, maybe someone can give you a solution with better performance than a loop.

http://msdn.microsoft.com/en-us/library/ms178642%28v=sql.100%29.aspx
0
 
LVL 6

Accepted Solution

by:
esolve earned 500 total points
ID: 38827067
DECLARE @TmpTable TABLE (ID INT IDENTITY(1,1), Name VARCHAR(50))
INSERT INTO @TmpTable (Name) values ('James')
INSERT INTO @TmpTable (Name) values ('Pete')
INSERT INTO @TmpTable (Name) values ('Sarah')
INSERT INTO @TmpTable (Name) values ('Carina')
INSERT INTO @TmpTable (Name) values ('Shane')

--select * from @TmpTable

DECLARE @IteratorID INT
SET @IteratorID = 0

WHILE @IteratorID IS NOT NULL
BEGIN
      --SELECT ROW TO WORK WITH
      SET @IteratorID = (SELECT TOP 1 ID FROM @TmpTable)
      
      IF(@IteratorID IS NOT NULL)
      BEGIN
      ------------
            --DO WHAT YOU WANT WITH THIS ROW
            DECLARE @Name NVARCHAR(50)
            SET @Name = (SELECT Name FROM @TmpTable WHERE ID = @IteratorID)
            SELECT @Name
      ------------
      END
      
      --IMPORTANT: DELETE ROW TO ENSURE YOU'RE DONE WITH IT AND PREVENT STACK OVERFLOW
      DELETE FROM @TmpTable WHERE ID = @IteratorID
END
0
 

Author Comment

by:ravichand-sql
ID: 38841614
DECLARE @TmpTable TABLE (ID INT IDENTITY(1,1), Name VARCHAR(50))
INSERT INTO @TmpTable (Name) values ('James')
INSERT INTO @TmpTable (Name) values ('Pete')
INSERT INTO @TmpTable (Name) values ('Sarah')
INSERT INTO @TmpTable (Name) values ('Carina')
INSERT INTO @TmpTable (Name) values ('Shane')

Instead of inserting data using INSERT statement can I write a SELECT query for bulk insert ??

Like,

SELECT name into @TmpTable  FROM tblSomeName  WHERE condition
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question