title: Pass a table name to stored procedure as a parameter
body: <p>This is my stored procedure, this is working:</p>
  
  <pre><code>/* 
      @CRM_Ref - This is the only required input port for this SP
      @North_Ref - Output port, returns the North_Ref for a valid CRM_Ref input
      @Output_Message - Output port, Returns the output of the SP, either 'Success' or 'North_Ref not found'
  */
  
  ALTER PROCEDURE [dbo].sp_Get_North_Reference 
      @CRM_Ref NVARCHAR(255), 
      @North_Ref VARCHAR(255) OUTPUT, 
      @Output_Message VARCHAR(255) OUTPUT
  AS
      DECLARE @var_North_Ref VARCHAR(255); -- Variable used to store the North_Ref
      DECLARE @var_Output_Message VARCHAR(255); -- Variable to carry the Output_Message
      DECLARE @COUNTER INT; -- Counter for the amount of times the while loop should run
  
      SET @COUNTER = 100;
  
      -- Loop will run 10 times with a 10 second delay between each loop
      WHILE @COUNTER &gt;= 1
      BEGIN
          SET @var_North_Ref = (SELECT TOP 1 North_Ref FROM DEV.dbo.Address__ADDRESS WHERE CRM_Ref =  @CRM_Ref ORDER BY PUBLICATION_INSTANCE_DATE DESC)
          IF @var_North_Ref IS NULL
          BEGIN
              SET @COUNTER = @COUNTER - 1; -- Counter is decremented by 1
              SET @var_Output_Message = 'North_Ref not found';
              WAITFOR DELAY '00:00:10'; -- Wait is triggered if no North_Ref is found
          END
          ELSE
          BEGIN
              SET @COUNTER = 0; -- Counter is set to 0 to end the while loop
              SET @var_Output_Message = 'Success';
          END
      END
  
  SET @Output_Message =  @var_Output_Message; -- Format Output_Message
  SET @North_Ref = @var_North_Ref; -- Format North_Ref
  
  ;
  
  GO
  </code></pre>
  
  <p>I would like to add another parameter into this stored procedure (<code>@TableName VARCHAR(255)</code>) which I want to pass to the <code>SELECT</code> statement.</p>
  
  <p>So I would like something like:</p>
  
  <pre><code>SELECT TOP 1 North_Ref 
  FROM @Table_Name 
  WHERE CRM_Ref =  @CRM_Ref 
  ORDER BY PUBLICATION_INSTANCE_DATE DESC
  </code></pre>
  
  <p>I have tried doing this as it is above but I am getting errors as I don't think you can use parameters as a table name in stored procedures </p>