Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

question about calling a stored procedure 1

Status
Not open for further replies.

mollytu

Programmer
Joined
Jan 4, 2010
Messages
7
Location
US
Hi,

How do you call a stored procedure to run for every row in a table? For example:

EXEC myProc @field1, @field2, @outVar OUT, @outVar OUT

against all the rows in a given table?

Thank you
 
Just define a proper WHERE clause in the SP and pass parameter to it.

Borislav Borissov
VFP9 SP2, SQL Server 2000/2005.
 
That is a good idea, but I'm not allowed to modify the sp.
 
You may want to post the SP. If you want to return all rows, you may need to pass % for parameters, but it's hard to tell without knowing the SP code.
 
What about just looping the proc through each row in the table? That was my idea.
 
SQL Server is best when used as SET BASED (which means no looping).

Perhaps you can transform your SP into user defined function and if so, you would be able to run it for all records in a select statement.
 
Markros, could you show a little example of what you mean... like an inline function?
 
Depends on what your SP was doing, but here is a quick example
Code:
create function dbo.MinValue(field1 int, field2 int) return int
as 
  begin
    if field1 > field2
       return field2
    else
       return field1
   end

select T.Field1, T.Field2, dbo.MinValue(T.Field1, T.Field2) as MinValue from myTable

This is a very quick sample from the top of my head - not tested at all.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top