?
Solved

Looping through the records

Posted on 2013-01-27
4
Medium Priority
?
276 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 93

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 1500 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

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this article I will describe the Backup & Restore 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.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

840 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