Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Execute Stored Procedure Multiple Times


Execute Stored Procedure Multiple Times

Author
Message
BSavoie
BSavoie
Old Hand
Old Hand (327 reputation)Old Hand (327 reputation)Old Hand (327 reputation)Old Hand (327 reputation)Old Hand (327 reputation)Old Hand (327 reputation)Old Hand (327 reputation)Old Hand (327 reputation)

Group: General Forum Members
Points: 327 Visits: 788
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?

.
Kingston Dhasian
Kingston Dhasian
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3001 Visits: 4995
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/
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 8957 Visits: 19015
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
Exploring Recursive CTEs by Example Dwain Camps
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)

Group: General Forum Members
Points: 44994 Visits: 39880
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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Eugene Elutin
Eugene Elutin
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3042 Visits: 5478
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
BSavoie
BSavoie
Old Hand
Old Hand (327 reputation)Old Hand (327 reputation)Old Hand (327 reputation)Old Hand (327 reputation)Old Hand (327 reputation)Old Hand (327 reputation)Old Hand (327 reputation)Old Hand (327 reputation)

Group: General Forum Members
Points: 327 Visits: 788
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.

.
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 8957 Visits: 19015
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) 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
Exploring Recursive CTEs by Example Dwain Camps
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search