title: Insert data into a table from Open Query with variable
body: <p>I am trying to using OPENQUERY to pull some data into a table. Here's what my code looks like:</p>
  <pre><code>  DECLARE @TSQL VARCHAR(MAX)
    DECLARE @CD   VARCHAR(10) = 'XX'
  
    DECLARE @OracleData TABLE  (Cd VARCHAR(20), ApptDATE Datetime )
    
    INSERT INTO @OracleData(Cd,ApptDATE )
  
    SELECT  @TSQL = 'SELECT * FROM OPENQUERY(LinkedServer,''Select p.Cd, p.AppDate
                                                          from ta.table1 p 
                                                          where p.IdCode = ''''' + @CD + ''''''')'
  
    EXEC (@TSQL)
  </code></pre>
  <p>I end up with the following error:</p>
  <blockquote>
  <p>An INSERT statement cannot contain a SELECT statement that assigns
  values to a variable.</p>
  </blockquote>
  <p>When I attempt to run the <code>EXEC(@TSQL)</code> without the <code>INSERT</code> it works like a charm, but I am unable to do an insert.</p>
  <p>Any ideas how I can possibly resolve this?
  Thanks.</p>