What is a Cursor?

  • This is my first post in this forum and primarily joined to accelerate the sql server learning experience. One question I have been researching is on Cursor's and how they are used.

    Can anyone recommend a good reference for me to read and learn more regarding cursors and programming the sql language in general?

    Much appreciated.

    Keith

  • Only if you promise to avoid cursors and while loops like they were going to kill you... because they'll certainly kill the performance of your code. There's almost always an alternative to the procedureal code (I call it "RBAR"... see my signature line below). SQL Server responds best to "set based programming". The only difference between the ever slothful procedureal programming methods found in cursors and while loops and "set based programming" is that you must train yourself to think in columns rather than rows. Sounds pretty simple... but most developers never really get it and they end up resorting to cursors and while loops.

    There's a couple of places to "learn stuff"... if you really want to learn about SQL Server, you came to the right place. You should also visit the online help for SQL Server... it comes free with SQL Server and it's called Books Online. Most folks on this forum just call it "BOL"... it IS the bible for SQL Server. I'd lookup cursors there to see how they work.

    My favorite "beginners" tutorial for SQL is at W3Schools.com ... just be aware that's ANSI SQL and not SQL Server. SQL Server will do most of the ANSI stuff... but there's so much more than just ANSI.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply