converting cursors to set based procedures

  • i know each suituation is different, but are there any rules/steps to go through or things to look out for when converting a cursor into set based logic?

  • Obviously, When the situation deals with a lot of records, it is always best to use the set based logic rather persisting with CURSORS. Using Set based logic would give a better edge over the cursor option.

  • the critical obvious one is that you still get the same end result - I know this sounds rather silly but knowing what you should get and making sure you do when you change code is important.

    I'd also make sure that there are savings made - I'm not a great lover of cursors, but there's no point spending hours or weeks to change a piece of code just for the sake of it - and of course remember "if it ain't broke don't fix it!"

    [font="Comic Sans MS"]The GrumpyOldDBA[/font]
    www.grumpyolddba.co.uk
    http://sqlblogcasts.com/blogs/grumpyolddba/

  • Not exactly sure what u mean by set based however i like using home made cursors as oppose to implimenting real cusors.

     

    load into a mem table with what would normally be loaded into the cursor w/ an added field at the end labeled processed and this gets set to zero for every record

     

    while exists ( select top 1 * from mem_table where processed - 0)

     

    begin

    Store all fields from the record your working w/ into local variables

    do whatever u would normally do w/ these values

     

    update memtable

    set processed = 1

    where id =id

     

    end

     

    these are really useful

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

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