Execute Function for Selected Rows In A Table

  • I have a script that needs to be executed for all rows in a table. The script can be made into a function; however, I am curious as to options for executing it for a fairly large number of rows in a table. Thanks in advance.

  • Well normally you would just call the function in the field list of a query.

    SELECT fn_Function(FieldA)

    FROM Table1

    WHERE FieldA IS NOT NULL

    Normally you are just trying to get data back but it should work the way I think you want it to. Just throw away the output afterwards. Or return an error code or something.

    Kenneth FisherI was once offered a wizards hat but it got in the way of my dunce cap.--------------------------------------------------------------------------------For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/[/url]For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/[/url]Link to my Blog Post --> www.SQLStudies.com[/url]

  • Thank you for the reply. Would your answer change if a stored procedure was used as opposed to a function? What I am looking to do is select all ProductIDs from table A and feed them to a procedure as follows:

    EXECUTE dbo.sp_Procedure @ProductID, @Result OUTPUT

    A cursor and While loop could be utilized; however, I would rather use a set-based approach if able to.

    Thanks in advance.

    Ken

  • The only thing I could add is that maybe you could put the 'EXECUTE dbo.sp_Procedure' line inside the function, and then using the function in your select statement. Not really sure what the advantage of that over just writing the stored procedure as a function though.

  • Thanks for the reply.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply