title: Conversion failed when converting the varchar value to data type int - but I'm not converting anything
body: <p>I am trying to write a stored procedure that takes two parameters: table name and record ID. </p>
  
  <p>It must return a record with a specified ID (<code>@FormID</code>) from the table with the specified name (<code>@TableName</code>). </p>
  
  <p>I get this error: </p>
  
  <blockquote>
    <p>Conversion failed when converting the varchar value 'SELECT * FROM [Form12_AuditLog] WHERE [FormID] = ' to data type int."</p>
  </blockquote>
  
  <p>I can't really understand the issue because I'm not trying to convert anything to data type int.</p>
  
  <p>The parameters passed to the SQL are: </p>
  
  <pre><code>@FormID = 88
  @TableName = Form12_AuditLog  
  </code></pre>
  
  <p>SQL:</p>
  
  <pre><code>USE [MyDatabase]
  GO
  /****** Object:  StoredProcedure [dbo].[GetAuditLogByFormID]    Script Date: 20/12/2016 5:50:53 PM ******/
  SET ANSI_NULLS ON
  GO
  SET QUOTED_IDENTIFIER ON
  GO
  ALTER PROCEDURE [dbo].[GetAuditLogByFormID] 
      @TableName varchar(50),
      @FormID integer
  AS
  BEGIN
      SET NOCOUNT ON;
  
      DECLARE @ActualTableName AS varchar(255)
  
      SELECT @ActualTableName = QUOTENAME( TABLE_NAME )
      FROM INFORMATION_SCHEMA.TABLES
      WHERE TABLE_NAME = @TableName   
  
      DECLARE @sql AS NVARCHAR(MAX)
      SELECT @sql = 'SELECT * FROM ' + @ActualTableName + ' WHERE [FormID] = ' + @FormID  + ';'
  
      EXEC(@sql)
  END
  </code></pre>