Simple SQL Cursor Example

Simple SQL Cursor Example

If you’re reading this blog, chances are, you know how to write/use a SQL cursor. But this is more for my benefit, and maybe even yours. Just a quick cursor example, so that I can easily reference the syntax if I ever need to quickly look it up, since I always seem to forget it.  Enjoy.


--will need to declare at least one variable
--in this case, to store values when iterating through cursor
DECLARE SIMPLE_CURSOR CURSOR FOR SELECT ID FROM CONTACT OPEN SIMPLE_CURSOR FETCH NEXT FROM SIMPLE_CURSOR --Start the cursor INTO @CONTACTID WHILE @@FETCH_STATUS = 0 --while there is a loaded record, keep processing BEGIN --do whatever you need to do print ('This is where the magic happens! Do whatever you need to do (update/insert/delete/stored proc/etc.') FETCH NEXT FROM SIMPLE_CURSOR INTO @CONTACTID --fetch next record END CLOSE SIMPLE_CURSOR --close and deallocate DEALLOCATE SIMPLE_CURSOR

There are 11 comments .


Short and quick understandable explanation… 🙂

Reply »
mehrnoosh —

thanks,very good

Reply »
Shanaka —

i am not familiar with the sql CURSOR. can you just give a idea, why we are using this CURSOR option on sql.


Reply »

    Hi Shanaka,

    Cursors can be handy when you need to iterate over some data set of records, and perform an action for each record. For example if you want to update each row with some sort of calculation, etc.

    There are many ways to do this, but the SQL Cursor is one of them.

    Hope that helps

    Reply »
W.Devonshire —

Many thanks, can never remember the cursor syntax 🙂


Reply »
cinafre —

I have multiple values in one cell in a table which are separated by an space from each other. this is how my table looks like, there is a space in between each string in every cell:

column1 column2
Data1 Data2 Related1 Related2

I want to have another table in which “Data1” and “Related1” are in one row, “Data2” and “Related2” are in another row, because they are related to each other:

column1 column2
Data1 Related1
Data2 Related2
How can I do that?

Reply »
Mritunjay kumar —

It is quick Understable example

Reply »
Sharad Patil —

Its Very useful code
Thank u!

Reply »

Share Your Thoughts!


What My Clients Say

Karin - Hope and Future

Brent has always been very prompt and generally exceeds our expectations with his work. His maintenance fee is very reasonable. I strongly recommend him!  We get many compliments on our beautiful web site!


I contacted Krueger Web Design and Brent did everything.  He setup a test site with the changes so we could verify everything was correct before we transferred from my old hosting provider.  During the setup and conversion Brent was extremely professional and responsive. He responded to any issues the same day.

Dan Kaufman
Emily - GFCLT

We've had a great experience working with Krueger Web Design. Prompt responses, great ideas, effective work... all at a reasonable price! This was the perfect fit for our organization.

Mahmud - ACMC

I would love to recommend you to anyone! You have been very helpful when we needed you the most.

Brian - SEMNIC

Working with Krueger Web Design was effortless. Communicating what I wanted for the website and the results that I saw were identical. The customer service was always prompt and I am completely satisfied with the product.

Zach - Stadium Club

Exactly what I wanted! Fast turnaround time and very affordable. Answered all of our questions quickly and made adjustments promptly. Very easy to work with and puts everything into terminology that even I can understand!

Contact Krueger Web Design

Have a question for me? Need an estimate on some work? You can email me at or drop me a line using the button below:

Copyright © 2018 Krueger Web Design - All rights reserved
Web Design in Madison, WI