Execute Stored Procedure for List of Parameters in SQL

Saeid picture Saeid · Apr 23, 2012 · Viewed 12.2k times · Source

I have an Stored Procedure that have an argument named Id:

CREATE PROCEDURE [TargetSp](
   @Id  [bigint]
)
AS 
BEGIN
   Update [ATable]
   SET [AColumn] = 
   (
     Select [ACalculatedValue] From [AnotherTable]
   )
   Where [ATable].[Member_Id] = @Id
END

So I need to use it for a list of Id's not for one Id like :

Exec [TargetSp]
  @Id IN (Select [M].[Id] From [Member] AS [M] Where [M].[Title] = 'Example');

First: How can I Execute it for a list?

Second: Is there any Performance difference between I execute the sp many times or rewrite it in target script?

Answer

Dan picture Dan · Apr 23, 2012

You could use a table-valued parameter (see http://msdn.microsoft.com/en-us/library/bb510489.aspx). Generally, if you send only one request to the server instead of a list of requests you will see a shorter execution time.