Decrease Font Size
Increase Font Size
   BLOG

SQL 2005 Cursor Template

SQL 2005 cursor template

SQL Server 2005 cursor example

SQL SERVER 2005 TSQL Cursors

Once in a while, we might have to use a cursor in our stored procedure. Normally, I will keep the template in my folder and retrieve it whenever I need to consume it. It saves me some time trying to recollect the complete syntax. Here is the template with a brief example, hope someone will find it useful.

SET NOCOUNT ON
GO

--sample table
IF OBJECT_ID('tempdb..#MyTempTable') IS NOT NULL
 BEGIN
	DROP TABLE #MyTempTable
    CREATE TABLE #MyTempTable (
		column1 varchar(10),
		column2 varchar(10),
		column3 varchar(10)
	)
 END
--Insert dummy data
INSERT INTO #MyTempTable (column1, column2, column3) values('apple', 'apple2', 'apple3')
INSERT INTO #MyTempTable (column1, column2, column3) values('banana', 'banana2', 'banana3')
INSERT INTO #MyTempTable (column1, column2, column3) values('grape', 'grape2', 'grape3')

--Template start here
DECLARE @Column1 varchar(10), @Column2 varchar(10)

DECLARE db_cursor CURSOR FOR 
SELECT column1, column2 FROM #MyTempTable

OPEN db_cursor  
FETCH NEXT FROM db_cursor INTO @Column1, @Column2

WHILE @@FETCH_STATUS = 0  
BEGIN  
	
	  --do work here	
      print @Column1 + ' ' + @Column2

	FETCH NEXT FROM db_cursor INTO @Column1, @Column2
END  

CLOSE db_cursor  
DEALLOCATE db_cursor
--End template

Comments (13) -

  • Stop Dreaming Start Action

    7/5/2009 10:36:33 AM |

    Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon.

  • bryian

    7/6/2009 4:50:58 PM |

    thanks!

  • bamboo floors

    11/12/2009 11:00:15 AM |

    Which software can I use to make Blogger templates?

  • Barbera del monferrato

    12/14/2009 12:08:02 AM |

    This blog contains really good stuff.Thanks for sharing this interesting blog.

  • meilleurs sites de casino

    12/15/2009 4:13:16 AM |

    SQL by itself is not the end-all and be-all for analytics, but it’s more powerful than I think you’re giving it credit for — particularly with newer architectures that provide parallelizationuser-defined functions, and map-reduce functionality.

  • fast payday loans

    12/21/2009 6:04:58 PM |

    The ability to concentrate and to use time well is everything.

  • Tourist Destinations

    2/23/2010 2:23:43 AM |

    Every one of us possesses fascination to visit different places so as to feel the thrill and excitements associated with such exotic tourist destinations.

  • business continuity templates

    2/25/2010 1:04:40 AM |

    I’m impressed, you know what you’re talking about

  • Electric Adjustable Beds

    3/8/2010 2:35:21 AM |

    The blog is in reality the great on this worthy subject. I match in together with your conclusions and looking forward to your coming updates. Just saying thanks will not just be enough, for the fantastic clarity inside your writing. I've grabbed your rss feed to remain informed of any updates. Gratifying work and much achievement inside your company dealings!

  • RocketVIP

    12/5/2010 2:01:07 AM |

    Good article! Thanks for your post

  • James

    12/26/2010 8:16:47 AM |

    Thank You. For Sharing This Articles

  • Welcome marrakech

    1/7/2011 1:24:43 AM |

    thanks

  • Amand

    1/8/2011 7:30:19 AM |

    Thank! for good post. Useful for me and another one.

Add comment

Loading