Santry Technology Solutions, Content Management, DotNetNuke, SharePoint Consulting
Register | Login
Wednesday, January 07, 2009

Sections
  
About Us
  
Partners
Downloads
  
 WWWCoder.com Resource Directory

Code Snip: Using Cursors in TSQL
2/3/2005 4:24:13 PM

In this article we will show how to iterate through a recordset in TSQL using cursors. In this example, we will select some records and then insert the ID into a related table.

Have you ever want to loop through a recordset and then perform another database operation as you go through the recordset? You may have considered doing this in the ASP.NET page, but it can really slow down your application making repeated calls over the network to connect to the database server from the Web server. In this code snip, we going to show you how you can implement cursors in your stored procedures in SQL.

Alter PROCEDURE dbo.sp_InsertRecords
AS
Declare @id int
-- first we'll pull some records out of our table.
DECLARE CursorQuery CURSOR FOR
            SELECT ID FROM myTable WHERE MyRecord > 15
OPEN CursorQuery
-- we're going to fetch our record into the ID variable which we'll use for inserting a related record.
FETCH NEXT FROM CursorQuery
INTO @ID
PRINT 'Record Status' +  CAST(@@FETCH_STATUS as varchar)
WHILE @@FETCH_STATUS = 0
BEGIN
-- now insert the id from the main table into the related table.
            INSERT INTO
                        RelatedTable
                        (relatedID)
            VALUES
                        (@id)                   
            FETCH NEXT FROM CursorQuery
            INTO @ID
END
CLOSE CursorQuery
DEALLOCATE CursorQuery
RETURN

Basically what we're doing here is executing a SQL statement that is going to return a recordset, and then declare a cursor so we can iterate through the records. As we iterate through the records we will insert the ID field value into the RelatedTable's relatedID field. Just remember that you need to FETCH the value into your variable in this case @ID.

 


Page Options:
format for printing  Format for Printer
email article  Email Page
add to your favorites   Add to Favorites
How would you rate the quality of this content?
Poor - - Excellent
Comments?
Overall Rating:
Comments Left:
Left on 8/4/2008 3:03:07 PM by Anonymous
Comments: goos stuff
Left on 7/31/2008 3:14:54 PM by Anonymous
Comments: This was excellent - short and to the point with a good example
Left on 3/26/2008 7:42:37 PM by Anonymous
Comments: Just what I was looking for, thanks.
Left on 7/24/2007 4:35:32 PM by Anonymous
Comments: I like ..just work great
Left on 2/28/2006 1:33:49 PM by Anonymous
Comments: Short, sweet, to the point. Thanks!
Left on 5/20/2005 8:19:50 AM by Anonymous
Comments: klkkkkkklklllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllffffffffffffffffffffffffffffffffffffffffffffffffuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqggggggggggggggggggggggggggggg
Left on 2/24/2005 9:10:42 AM by Anonymous
Comments: Good enough and basic enough to understand.
  

 Latest Articles
  

 Latest News
  

 

Spotlight
Syndication

 


 


Digg This
 


DotNetNuke Platinum Benefactor

  
 

 Terms Of Use | Privacy Statement
 Copyright 2008 - Santry Technology Solutions, Box 172, Girard, PA 16417, (814) 774-0970