T-SQL question

  • Help appreciated:

    I open up a cursor to a [large table]. I have to run through each record , do some math on some of the fields based on entries in other fields yada yada.

    Then I open up a cursor to a [main table] where all the info is stored. I pull up a matching record based on a number (serial number ) then make some more calculations and then finally store the results in the [main table] using the current cursor position.

    I then close the cursor, deallocate then loop back to the beginning of the procedure to fetch the next record in the [large table] open the cursor to the [main table] etc etc.

    Question:  Should I use a Select and Update rather than a FETCH and UPDATE (speed being of biggest importance)

    Thanks in advance

    James

  • I would guess select into would be much faster than fetching

  • Show us the code, and we might help you rewrite it SET-based.

     


    N 56°04'39.16"
    E 12°55'05.25"

  • Nested cursors on large tables, awesome!!  If speed is your concern, scrap the cursors.  I agree with Peter, post your code and work towards creating a SET-based solution. 

    John Rowan

    ======================================================
    ======================================================
    Forum Etiquette: How to post data/code on a forum to get the best help[/url] - by Jeff Moden

  • update [main table]

    set somecolumn = someCalculations

    From [main table] A inner join

    (select id, someLargeTableCalculations

    from [LargeTable]

    ) B on A.someid = B.id

    as has been said already post your code if you need a better solution


    Everything you can imagine is real.

  • Well, what say thee, James... I see at least 3 people that really wanna help you fix this... all you need to do is post the offending code. 

    --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)

  • I got a co-worker to walk me through the code but greatly appreciate the offer for help.  sorry for the late reply I got tied up the rest of the afternoon. 

    People prob don't say it enough but the posters on here are awesome (this excludes the bashers that eat people up for not replying back to a post the same minute)

  • You feel "bashed" by anything on this thread?   Most folks express some urgency when they post a question and your's is likely interesting... All I was doing was showing that I was interested in your problem... 

    --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)

  • its a good kind of bashing because it shows people want to help you . maybe we should have a status on posts to denote urgency of a solution? that will probably appease the bashed and the bashers


    Everything you can imagine is real.

  • Heh... yeah... and I'm still wondering why anyone would even use the term "bashing" in relation to what I posted.  And I certainly wasn't trying to "eat at" anyone.

    So, James, did your compatriot end up using either a Cursor or a While loop to solve your problem?  Like I said, I am interested in your problem...

    --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 10 posts - 1 through 9 (of 9 total)

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