Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 2005
»
SQL Server 2005 General Discussion
»
Execute Stored Procedure Multiple Times
Execute Stored Procedure Multiple Times
Rate Topic
Display Mode
Topic Options
Author
Message
BSavoie
BSavoie
Posted Tuesday, February 26, 2013 8:48 PM
Old Hand
Group: General Forum Members
Last Login: 2 days ago @ 11:32 AM
Points: 309,
Visits: 720
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?
.
Post #1424314
Kingston Dhasian
Kingston Dhasian
Posted Wednesday, February 27, 2013 1:00 AM
SSCrazy
Group: General Forum Members
Last Login: Today @ 8:18 AM
Points: 2,230,
Visits: 4,197
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/
Post #1424370
ChrisM@Work
ChrisM@Work
Posted Wednesday, February 27, 2013 1:46 AM
SSCertifiable
Group: General Forum Members
Last Login: Today @ 9:27 AM
Points: 5,618,
Visits: 10,990
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
Post #1424394
Jeff Moden
Jeff Moden
Posted Wednesday, February 27, 2013 7:39 PM
SSC-Dedicated
Group: General Forum Members
Last Login: Today @ 7:51 PM
Points: 32,910,
Visits: 26,800
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 "
R
ow-
B
y-
A
gonizing-
R
ow".
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."
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #1424846
Eugene Elutin
Eugene Elutin
Posted Thursday, February 28, 2013 8:32 AM
SSCrazy
Group: General Forum Members
Last Login: Today @ 11:00 AM
Points: 2,543,
Visits: 4,384
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!"
(So many miracle inventions provided by MS to us...)
How to post your question to get the best and quick help
Post #1425071
BSavoie
BSavoie
Posted Thursday, February 28, 2013 8:12 PM
Old Hand
Group: General Forum Members
Last Login: 2 days ago @ 11:32 AM
Points: 309,
Visits: 720
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.
.
Post #1425330
ChrisM@Work
ChrisM@Work
Posted Friday, March 01, 2013 4:22 AM
SSCertifiable
Group: General Forum Members
Last Login: Today @ 9:27 AM
Points: 5,618,
Visits: 10,990
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
Post #1425432
« Prev Topic
|
Next Topic »
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.