Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Calling a Stored Procedure for each row of a query

Author  Topic 

mtl777
Yak Posting Veteran

63 Posts

Posted - 2012-10-26 : 20:15:35
Is there any way to call a stored procedure for each row of a query result set? Let's say you'd like to use the data of each row as the parameters for a stored procedure to be called. Is there any way to do that without having to use a cursor?

Thanks!

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-10-26 : 23:25:41
nope.
If there's a way alternatives to be considered are

1. convert stored procedure to function so that you can call it using APPLY operator for set based operation.

see scenario 4 here

http://visakhm.blogspot.com/2010/01/multipurpose-apply-operator.html


2. Alter procedure to accept a list of value and do processing with it

see
http://vyaskn.tripod.com/passing_arrays_to_stored_procedures.htm

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

mtl777
Yak Posting Veteran

63 Posts

Posted - 2012-10-29 : 12:54:38
Thanks a lot Visakh! Very cool techniques especially Apply.

It would have been wonderful though if SQL Server could do exactly as I wanted. It's still not possible even with SQL Server 2012?
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2012-10-29 : 13:40:09
Normally you would write the stored procedure to operate on any number of rows. In other words, on sets of data, rather than one row/element of data. If you posted your code we might be able to demonstrate or rewrite it work that way.
Go to Top of Page

mtl777
Yak Posting Veteran

63 Posts

Posted - 2012-10-29 : 21:48:24
No application that I'm working on at the moment really. I was just wondering -- in case I needed to do something like it in the future. But one example comes to mind: Let's say you wanted to calculate the payroll for each employee in your company and the calculations are so complex, and furthermore, the results of the calculations are going to be inserted to a table and are going to be used to update other tables.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-10-30 : 09:50:25
quote:
Originally posted by mtl777

No application that I'm working on at the moment really. I was just wondering -- in case I needed to do something like it in the future. But one example comes to mind: Let's say you wanted to calculate the payroll for each employee in your company and the calculations are so complex, and furthermore, the results of the calculations are going to be inserted to a table and are going to be used to update other tables.


You can still write them in same procedure which accepts a list of employess. Then parse out list to temporary table to insert employee ids. then you can use set based technique using UPDATE to do required calculations and update them baack to any number of tables.
And for individual employee payroll you can pass single value in list and it would still work fine

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -