title: Obtaining stored procedure parameter dynamically by name
body: <p>Let's create a stored procedure with many similar input parameters with the same type:</p>
  
  <pre><code>CREATE PROCEDURE [dbo].[SampleStoredProcedure]
      @p1 NVARCHAR(MAX) = NULL,
      @p2 NVARCHAR(MAX) = NULL,
      @p3 NVARCHAR(MAX) = NULL,
      ...
      @p1000 NVARCHAR(MAX) = NULL
  </code></pre>
  
  <p>At the beginning, inside the procedure, I want to generate a list of parameters (strings) I should to use, which depends on some database data.</p>
  
  <p>For example, I have 1000 parameters, but I want to read only 10, and execute some SQL statement with these values.</p>
  
  <ol>
  <li><p>Is it possible to get the parameter by its name as string? Any dynamic SQL query?</p></li>
  <li><p>Maybe there is some way to iterate through all input parameters and check if the list contains the current one?</p></li>
  </ol>
  
  <p>It is possible to generate the SQL with some condition for each parameter separately but the stored procedure will be very long and difficult to modify later. Probably the performance will be worse than in 1. method too. Table Valued Parameter is a solution too, but the point is to avoid it, using separated parameters.</p>
  
  <p><strong>Summary:</strong>
  I have a list of strings defining which input parameters should be mapped and I just want to ignore others, to avoid additional conditions.
  The SQL query should be executed only with some input parameters - defined by a context in other table.</p>
  
  <p>Some example:</p>
  
  <pre><code>-- It will generate parameters' list - some context.
  DECLARE MyCursor CURSOR
      LOCAL STATIC READ_ONLY FORWARD_ONLY
  FOR
      SELECT [FieldName] FROM [Fields] AS [F] WHERE [F].[ProjectId] = @ProjectId
  
  -- It will execute some action only for defined input parameters.
  OPEN MyCursor
  FETCH NEXT FROM MyCursor INTO @FieldName
  WHILE @@FETCH_STATUS = 0
  BEGIN 
      -- It doesn't work, but it illustrates the problem.
      SET @FieldValue = N'@' + @FieldName
      EXEC sp_executesql @Query, @Params, @FieldName = @FieldName, @FieldValue = @FieldValue
      FETCH NEXT FROM MyCursor INTO @Field
  END
  CLOSE MyCursor
  DEALLOCATE MyCursor
  </code></pre>