Decrease Font Size
Increase Font Size
   BLOG

SQL 2005 Cursor Template

by bryian 2. August 2009 15:45

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

Tags: , ,

SQL

Comments

7/15/2009 12:36:33 AM #

Stop Dreaming Start Action

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.

Stop Dreaming Start Action Indonesia

7/16/2009 6:50:58 AM #

bryian

thanks!

bryian United States

11/22/2009 1:00:15 AM #

bamboo floors

Which software can I use to make Blogger templates?

bamboo floors United States

12/23/2009 2:08:02 PM #

Barbera del monferrato

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

Barbera del monferrato United States

12/24/2009 6:13:16 PM #

meilleurs sites de casino

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.

meilleurs sites de casino India

12/31/2009 8:04:58 AM #

fast payday loans

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

fast payday loans United States

3/4/2010 4:23:43 PM #

Tourist Destinations

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

Tourist Destinations United States

3/6/2010 3:04:40 PM #

business continuity templates

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

business continuity templates United States

3/17/2010 4:35:21 PM #

Electric Adjustable Beds

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!

Electric Adjustable Beds United States

12/14/2010 4:01:07 PM #

RocketVIP

Good article! Thanks for your post

RocketVIP United States

1/4/2011 10:16:47 PM #

James

Thank You. For Sharing This Articles

James United States

1/16/2011 3:24:43 PM #

Welcome marrakech

thanks

Welcome marrakech Morocco

1/17/2011 9:30:19 PM #

Amand

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

Amand United States