title: How to execute a SQL String that references a table variable?
body: <p>I have a table variable in SQL Server 2008</p>
  
  <pre><code>    DECLARE @specsAndModel TABLE
      (
          specName VARCHAR(50)
          ,specVal VARCHAR(50)
      )
      INSERT INTO @specsAndModel
      VALUES('[modelNumber]', 'F00-B4R')
  </code></pre>
  
  <p>Then, I later build a string called <code>@query</code>, which I ultimately try to pass into <code>EXECUTE</code>, as in the following example:</p>
  
  <pre><code>    DECLARE @query NVARCHAR(MAX);
      SET @query = 'SELECT specName, specVal FROM @specsAndModel'
      EXECUTE(@query)
  </code></pre>
  
  <p>However, SQL Server gives me the error message: <code>Must declare the table variable "@specsAndModel".</code></p>
  
  <p>After searching around, I think this might be related to the <a href="http://msdn.microsoft.com/en-us/library/ms187096(SQL.105).aspx" rel="noreferrer">execution context</a>, but I haven't been able to resolve the problem.</p>
  
  <p>Is it even possible for me to use a table variable in a call to the execute function?</p>