SQL SERVER – Simple Example of Cursor

UPDATE: For working example using AdventureWorks visit : SQL SERVER – Simple Example of Cursor – Sample Cursor Part 2

This is the simplest example of the SQL Server Cursor. I have used this all the time for any use of Cursor in my T-SQL.
DECLARE @AccountID INT
DECLARE
@getAccountID CURSOR
SET
@getAccountID = CURSOR FOR
SELECT
Account_ID
FROM Accounts
OPEN @getAccountID
FETCH NEXT
FROM @getAccountID INTO @AccountID
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT
@AccountID
FETCH NEXT
FROM @getAccountID INTO @AccountID
END
CLOSE
@getAccountID
DEALLOCATE @getAccountID

Reference: Pinal Dave (http://www.SQLAuthority.com), BOL

SQL Cursor, SQL Scripts
Previous Post
SQL SERVER – Shrinking Truncate Log File – Log Full
Next Post
SQL SERVER – Query to find number Rows, Columns, ByteSize for each table in the current database – Find Biggest Table in Database

Related Posts

196 Comments. Leave new

  • hi this is rajasekaran

    Reply
  • Thanks for the simple demonstration! I was shown how to use cursors in a class years ago but never found an effective use for them as a VB.NET programmer (when you can make your own recordsets and twist them as you please). Now that I’m in an environment where I can’t use the nifty .NET data objects, a cursor was the only thing I could use to accomplish what I needed.

    There are a lot of other demos on the web that are a lot more difficult, but this really explains just the guts of how cursors work.

    Reply
  • Hi Pinal,
    1. How can we manage SQL Server in Clustered Environment?
    2. How can i stop and restart SQL Server services and instances in clustered environment?
    3. How can i stop and restart SQL server instances/services from windows ?

    If you have any documentation of managing SQL Server in clustered environment, please let me know.

    Thanks
    Abi

    Reply
  • hi
    pinal

    myself Dhirendra kumar jha
    i want to know that which action we have to take for performance tuning .

    thx

    Dhirendra

    Reply
  • hi,
    pinal

    For database and query optimization,
    what can i check that database is ok and query is ok.
    what kind of necessory command is required to find out these things.
    Please help me out

    Thanks & regards
    Dhirendra kumar
    mumbai

    Reply
  • Nice work. I keep finding this cursor as a good example for referencing.

    The Snow flake affect on you web page is interesting, but a little distracting.

    Reply
  • This is most excellent. I’ve been looking for just the bare bones of a cursor. This is a huge help to me. Did I mention it was huge?? :-)

    Thanks,
    Hosmerica

    Reply
  • Faisal Ahmed Qureshi
    January 13, 2008 4:56 pm

    Hi,
    its a nice example of Cursor.

    Regrads
    Faisal Ahmed Qureshi

    Reply
  • Hi
    This is most excellent. I’ve been looking for just the bare bones of a cursor. This is a huge help to me. Did I mention it was huge??

    Regards
    Sunil

    Reply
  • Hi,
    Its a very good sample code that explaining about cursor.
    Thank alot…

    Regards,
    Googul…….

    Reply
  • hi,
    this is vanphan.
    it’s a very good sample code that explaining about cursor
    th’s

    vanphan.

    Reply
  • Excellent one!!!

    Reply
  • Thanks dud.. good and very clear example

    Reply
  • This the best code sample i have ever found. thanks a lot for this. this is really useful.

    Reply
  • Thanxxx.
    It’s so simple and good example.

    Reply
  • what is the maximum size of storeprocedure in sql server 2000

    Reply
  • Hi,

    Pinal

    You are really amazing

    Reply
  • Hi, That was really a good example for cursor. But as I am new to SQL server. So can you tell me what changes should be required if we need to get multiple columns.

    Reply
  • Rajesh Erasani
    March 27, 2008 12:45 am

    Wow, Thats a great help. Really appreciate it.

    Reply
  • Thanks

    Reply

Leave a Reply