Home Forums SQL Server 2008 T-SQL (SS2K8) Convert the stored procedure to "Standard SQL Select" RE: Convert the stored procedure to "Standard SQL Select"

  • Nope... it's absolutely solid "advice" in its purest form. You just don't see it, yet. You're worried about solving the problem all at once and can't see the trees for the forest. 😉 You need to peel just one potato at a time. I'll show you what I mean. Let's see if we can walk you into solving this problem.

    Look at the code you posted. Following what I said about thinking in columns in my signature line, what do you see in common in each SELECT for each variable being populated in the cursor? The first step would be to write a SINGLE select using the COMMON join criteria that's available in all 4 similar queries.

    What should be in the SELECT list itself? That's simple. Remove the COUNT(1) and forget about it for right now. Move all the columns in the WHERE that are not common in all 4 queries up to the SELECT list. Once you've done that, post the code here and we'll show you what's next.

    And, don't jump ahead. Remember, we're peeling one potato at a time here so that you learn the steps.

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