title: Must declare the scalar variable @RECEIVABLESDATA
body: <p>I have this SQL code that I'm using to insert 3 tables at a time dynamically. When I try to run it, I get this message </p>
  
  <blockquote>
    <p>Msg 137, Level 16, State 1, Procedure InsertData, Line 28
    Must declare the scalar variable "@RECEIVABLESDATA".</p>
  </blockquote>
  
  <p>Any ideas?</p>
  
  <pre><code>USE [PantaRei]
  GO
  
  SET ANSI_NULLS ON
  GO
  
  SET QUOTED_IDENTIFIER ON
  GO
  
  CREATE PROCEDURE [dbo].[InsertData] 
  @RECEIVABLESDATA RECEIVABLESTABLE READONLY,
  @DILUTIONSDATA DILUTIONSTABLE READONLY,
  @ACCOUNTABLESDATA ACCOUNTABLESTABLE READONLY,
  @TABLE1 VARCHAR(MAX),
  @TABLE2 VARCHAR(MAX),
  @TABLE3 VARCHAR(MAX)
  AS
  BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      DECLARE @CMD nvarchar(345)
  
      SET NOCOUNT ON;
  
      SET @TABLE1 = RTRIM(@TABLE1)
      SET @TABLE2 = RTRIM(@TABLE2)
      SET @TABLE3 = RTRIM(@TABLE3)
  
      SET @CMD =
  
          'INSERT INTO ' + QUOTENAME(@TABLE1) +
          'SELECT * FROM [DBO].' + QUOTENAME(@RECEIVABLESDATA)
  
      EXECUTE sp_executesql @CMD
  
      SET @CMD =
  
          'INSERT INTO ' + QUOTENAME(@TABLE2) +
          'SELECT * FROM [DBO].' + QUOTENAME(@DILUTIONSDATA)
  
      EXECUTE sp_executesql @CMD
  
      SET @CMD =
  
          'INSERT INTO '+ QUOTENAME(@TABLE3) + 
          'SELECT * FROM [DBO].' + QUOTENAME(@ACCOUNTABLESDATA)
  END
  </code></pre>