views:

139

answers:

5

If I want to execute stored procedure using the values returned from result set of a select statement. So number of times SP should get executed is equal to the number of result set from the select statement.

Is there any other way than using a cursor to do the above?

UPDATE
Can anyone please give sample code with While loop at least?

A: 

An alternative to a cursor is a while loop, which is sometimes recommended as an alternative to SQL Cursors.

Is the problem that you want to avoid using the cursor, or is it that you are wating to avoid iteration altogether?

James Wiseman
Can you please give sample code with While loop at least?
Ismail
I wanted to avoid iteration. But I think there should not be a problem in using `while loop`
Ismail
A: 

Maybe this could help you, create a UDF and then call the stored proc from within that UDF. Since you can call UDF in a select query, it should execute the stored proc as many times as you have results in select query.

Sachin Shanbhag
i thought that, but i am not sure that, UDF equal to SP. I think UDF is more limited than SP. Can we create a temp table and manipulate data and insert them to temp table in UDF?
bahadir arslan
You can't call stored procedures directly from within UDF's (though it is possible with an unscalable hack that opens up a new connection and calls it via `openrowset`)
Martin Smith
i think we can call only extended store procedure in UDF
KuldipMCA
@bahadir arslan - you can't create temp tables in a UDF but you can use table variables.
Jeff O
Jeff O, you are right. Well then can we insert row a physical table in UDF? I don't think so.
bahadir arslan
This would have been great solution only if it was possible ;) !!!
Ismail
+1  A: 

Hi, In T-SQL there are only 2 ways for iteration. While loop or cursors. If you don't want to use cursors, you had to use while loop as James Wiseman said.

ANother way to accomplish this situation is SQL CLR. If you are using SQL CLR, you can use all C# (or VB.Net) iterations to reach your goal.

bahadir arslan
SQL CLR is not an option with me.
Ismail
+1  A: 

You'll have to convert your proc to a Multi-Statement Tabled Value UDF..

create function dbo.udf_Whatever_That_Proc_Did(
     @SameOldParam as int
)
AS Begin
Declare --same variables here

/*same code in your proc that does not 
    - invoke nondeterministic built-in function
    - change state of database
    - return messages to caller
*/

Return
End

To utilize function:

Select * 
from dbo.udf_Whatever_That_Proc_Did(9999)
Jeff O
Can you please give sample code for it?
Ismail
+1  A: 

I would convert the proc to use a table variable and pass the data set in using that. The beauty of this is that once you have made the change, you can use the same proc for either single row inserts or mulitple and do it in sets not row-by-row. You need SQL Server 2008 for this one.

HLGEM
How would you use the values within stored proc?
Ismail
Lots of suggestions for set-based ways to process data here:http://wiki.lessthandot.com/index.php/Cursors_and_How_to_Avoid_Them
HLGEM
+1 for the link
Ismail
The link in comments gives most of the answers I suppose. So marking this as answer.
Ismail