Execute Stored Procedure Multiple Times

  • We have a sproc that has been around a long time, it's ben tweaked & prodded, etc over the years to where it's a critical piece of complicated business logic. I have a need to write a utility that executes this sproc for each row in another select statement. I've had the "cursors are evil" philosophy pounded into my brain over my many years to the point where I avoid them if for no other reason than embarrassment. So I've got it working in a while loop. I know it's not much better but it seems ok. I've run into this several times over the years. Is there a better way? For simplicity sake, let's say I have a sproc that takes a "CustomerId", and I need to feed the results of a "select customerId from bla bla bla" statement to this sproc. Is a while loop a good way to handle this sort of thing?

    .

  • If there is a requirement where calling the stored procedure becomes unavoidable, a CURSOR or WHILE loop approach is fine and can't be avoided

    But, if the requirement can be satisfied by creating some script which doesn't call the stored procedure altogether and which doesn't use CURSORS or WHILE LOOP's, that approach will be better.


    Kingston Dhasian

    How to post data/code on a forum to get the best help - Jeff Moden
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • It might be worth converting (a version of) the sproc into a multi-statement table-valued function. The performance won't change but it would give you the flexibility of "running" the code as an APPLY block within a query.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • ChrisM@Work (2/27/2013)


    It might be worth converting (a version of) the sproc into a multi-statement table-valued function. The performance won't change but it would give you the flexibility of "running" the code as an APPLY block within a query.

    +1

    --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 guess, if the rewriting your stored proc to work on a required set is not an option, use advise about converting proc into TVF and use it with CROSS APPLY - that will give you the best possible solution.

    However, if it's also not an option, choosing between CURSOR and WHILE LOOP is pointless. Do not bother! You will find no much difference between properly implemented CURSOR and WHILE LOOP. Both of them are two sides of the same RBAR coin 😉

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

  • Thanks all for the great advice. Rewriting the sproc is not an option at this time. Since this is just a wrapper around the real work horse, I guess it's really going to make little difference how the wrapper is coded. It seems like in this particular situation an RBAR solution is almost a clearer representation of what's going on. I'm finding that the real problem is that this sproc was not very well optimized, that's where I'm going to need to focus my time.

    I'll definitely keep the cross apply / function up my sleeve for next time. Although, a colleague of mine is trying to convince us to stay away from UDF's. Seems the optimizer really doesn't deal with them very well in most common situations. I think table value functions might be a little more optimizer friendly.

    .

  • BSavoie (2/28/2013)


    Thanks all for the great advice. Rewriting the sproc is not an option at this time. Since this is just a wrapper around the real work horse, I guess it's really going to make little difference how the wrapper is coded. It seems like in this particular situation an RBAR solution is almost a clearer representation of what's going on. I'm finding that the real problem is that this sproc was not very well optimized, that's where I'm going to need to focus my time.

    I'll definitely keep the cross apply / function up my sleeve for next time. Although, a colleague of mine is trying to convince us to stay away from UDF's. Seems the optimizer really doesn't deal with them very well in most common situations. I think table value functions might be a little more optimizer friendly.

    If you're lucky, creating the multistatement table-valued function might require little more than a change to the object type in the CREATE script for the stored procedure. Obstacles would include temporary tables, which would need to be changed to table variables, and dynamic SQL.

    Inline and multistatement TVF's (and inline scalar functions[/url]) are dealt with by the optimiser quite nicely. A quick test might take you no more than a few minutes and would show you a) if it's a straightforward process and b) the execution plan.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

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

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