Cursors

  • Hello,

    What exactly are cursors ? BOL isn't very clear on the topic.

    thanks

    J

  • Basically a mechanism to navigate through a recordset record by record in T-SQL code.

    Lookup FETCH and @@FETCH_STATUS in BOL.

    Do you think you have a need to use a cursor?



    Once you understand the BITs, all the pieces come together

  • Thanks, no, I don't need one, I just wanted to understand what they do.

    J

  • Ahhhh, got it, just read BOL about Fetch. It is sort of like an array with pre-defined functions (next, first, last). Nice ...

    J

  • Best thing to do, is try it out yourself to gain understanding.

    Couple things about cursors...

    1. If you can accomplish task without a cursor, try to because a cursor is usually slower.

    2. Just get the data you need.

    3. Always CLOSE & DEALLOCATE.

    4. In cursors's definition, use FAST_FORWARD if just doing a 1-n record read only.

    4b. and / or use WITH (NOLOCK) hint on tables.

    5. Use LOCAL so alike named cursor may be used in sub-procedures.

    Maybe others can give some do & don'ts...



    Once you understand the BITs, all the pieces come together

  • I'm not sure if I should think that cursors are nice.

    When coming to SQL from some kind of procedural programming language most people think cursors are a great thing because the concept of processing one row at a time is familiar to them. So they start using cursors, go on using cursors and tahdah turn SQL Server into some network ISAM. SQL Server is supposed to work set oriented.

    So IMHO avoid cursors where you can, and use them when you must.

    Frank

    http://www.insidesql.de

    http://www.familienzirkus.de

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

Viewing 6 posts - 1 through 5 (of 5 total)

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