How do I create a looping stored procedure based on column input from another table?
-
Thursday, September 20, 2012 6:26 PM
Hi all,
Right now I am working on doing some calculations for a report. The T-SQL for this is only 106 lines long - so it's not a huge process at all. My question is this:
I'm doing this calculation for 8 distinct categories. Is there a way to write a stored procedure where I can put the names of those 8 categories into a table - and then feed them into a cursor or variable (or use FETCH) to set them equal to a parameter in a stored procedure and have that parameter loop through the 106 lines of code? (and put all 106 lines in the stored procedure)
Sort of like a table-based while loop...
Thanks!
All Replies
-
Thursday, September 20, 2012 6:29 PMModerator
You can invoke your stored procedure with a parameter in a cursor based loop.For every expert, there is an equal and opposite expert. - Becker's Law
My blog- Marked As Answer by Iric WenModerator Tuesday, October 02, 2012 8:16 AM
-
Sunday, September 23, 2012 5:27 AMModerator
Instead of stored procedure looping, just use dynamic SQL:
http://www.sqlusa.com/bestpractices/dynamicsql/
For additional assistance, post your code.
Kalman Toth SQL SERVER 2012 & BI TRAINING
New Book: Beginner Database Design & SQL Programming Using Microsoft SQL Server 2012- Marked As Answer by Iric WenModerator Tuesday, October 02, 2012 8:16 AM
-
Sunday, September 23, 2012 3:31 PMHave you tried a table valued parameter ?
Dave Ballantyne ---- http://sqlblogcasts.com/blogs/sqlandthelike/

