SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Run an SP against a table of IDs without looping
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

richardlaw
Yak Posting Veteran

United Kingdom
68 Posts

Posted - 05/17/2013 :  15:37:57  Show Profile  Visit richardlaw's Homepage  Reply with Quote
Hi

I've created a SP that returns a row of data based on an ID provided.

I'd now like to run the SP for each ID within another table, but ideally without using a loop.

Thoughts/ideas?

Thanks as always

James K
Flowing Fount of Yak Knowledge

3712 Posts

Posted - 05/17/2013 :  15:56:00  Show Profile  Reply with Quote
The ideal way would be to modify the stored proc (or create another stored proc) which joins the result set produced by the original stored proc to the table that contains the ID's.

Other than that, you would have to loop. When you think about it, that makes sense because the stored procedure is a unit of compilation - which produces a single result, and so joining with that cannot be done in a set-based manner.
Go to Top of Page

richardlaw
Yak Posting Veteran

United Kingdom
68 Posts

Posted - 05/17/2013 :  16:43:46  Show Profile  Visit richardlaw's Homepage  Reply with Quote
Thanks.

What would be best in this case (from a performance perspective) - a join with the SP, or a simple while loop?
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3712 Posts

Posted - 05/17/2013 :  16:45:48  Show Profile  Reply with Quote
If you join with the CODE IN THE STOREDPROC which results in a set-based query, that would most likely give you much better performance than a while loop.
Go to Top of Page

russell
Pyro-ma-ni-yak

USA
5072 Posts

Posted - 05/17/2013 :  17:56:50  Show Profile  Visit russell's Homepage  Reply with Quote
Agree 100% with James. Of course, if you need a more specific answer, show the proc.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000