Red Gate forums :: View topic - DECLARE CURSOR
Return to www.red-gate.com RSS Feed Available

Search  | Usergroups |  Profile |  Messages |  Log in  Register 
Go to product documentation

SQL Prompt code snippets forum

DECLARE CURSOR

Search in SQL Prompt code snippets forum
Post new topic   Reply to topic
Jump to:  
Author Message
aorozco



Joined: 19 Apr 2010
Posts: 1
Location: Canada

PostPosted: Mon Apr 19, 2010 3:54 pm    Post subject: DECLARE CURSOR Reply with quote

Hi, I was looking for a DECLARE CURSOR snippet but I couldn't find one, so I decided to create this basic one.

DECLARE $CURSOR$--@Variables

DECLARE /*Cursor name*/ CURSOR
FOR /*Select Statement*/
OPEN /*Cursor Name*/
FETCH NEXT FROM /*Cursor Name*/
INTO --@Variables

WHILE @@FETCH_STATUS = 0
BEGIN

/*SQL Statements*/

FETCH NEXT FROM /*Cursor Name*/
INTO --@Variables
END
CLOSE /*Cursor Name*/
DEALLOCATE /*Cursor Name*/

I hope it helps!


Last edited by aorozco on Mon Apr 19, 2010 5:02 pm; edited 1 time in total
Back to top
View user's profile Send private message
Giggles220



Joined: 07 Dec 2006
Posts: 114
Location: Albany, NY

PostPosted: Mon Apr 19, 2010 4:43 pm    Post subject: Reply with quote

Thanks, no matter how many times I need to write cursor code, I always cut and paste an example to make sure I get it all. This snippet will be very helpful.
Back to top
View user's profile Send private message
EdCarden



Joined: 25 Nov 2008
Posts: 88

PostPosted: Thu Mar 01, 2012 6:43 pm    Post subject: A Cursor Code Snippet? Reply with quote

DO you use cursors enough that you would benefit from having a Cursor code snippet in SQL Prompt?

Cursors are a necessary evil in SQL that more often then ot are heavily over used by traditional procedural/OOP developers. There are valid scenarios for using cursors but they are far and few. If your using cursors enough that having a predefined code snippet would be of benefit then you should look take some time to look at alternatives to cursor use.

BTW - I mean no offense with the above; just comenting on how you could help yourself improve things by avoiding excess cursor use.
Back to top
View user's profile Send private message
datacentricity



Joined: 18 Jul 2012
Posts: 18
Location: London

PostPosted: Thu Jul 19, 2012 9:02 am    Post subject: Reply with quote

An alternative view might be that if one uses cursors so rarely as to need a snippit to remeber all the correct syntax that's a good thing.
_________________
"Your mind is like a parachute, it works best when open" Frank Zappa
"Be wary of strong drink. It can make you shoot at tax collectors…and miss" Robert Heinlein
blog: http://datacentricity.net
twitter: @datacentricity
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic All times are GMT + 1 Hour
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group