title: SELECT INTO a table variable in T-SQL
body: <p>Got a complex SELECT query, from which I would like to insert all rows into a table variable, but T-SQL doesn't allow it.</p>
  
  <blockquote>
    <p>Along the same lines, you cannot use a table variable with SELECT INTO or INSERT EXEC queries.
    <a href="http://odetocode.com/Articles/365.aspx" rel="noreferrer">http://odetocode.com/Articles/365.aspx</a></p>
  </blockquote>
  
  <p>Short example:</p>
  
  <pre><code>declare @userData TABLE(
                          name varchar(30) NOT NULL,
                          oldlocation varchar(30) NOT NULL
                         )
  
  SELECT name, location
  INTO @userData
  FROM myTable
      INNER JOIN otherTable ON ...
  WHERE age &gt; 30
  </code></pre>
  
  <p>The data in the table variable would be later used to insert/update it back into different tables (mostly copy of the same data with minor updates). The goal of this would be to simply make the script a bit more readable and more easily customisable than doing the <code>SELECT INTO</code> directly into the right tables.
  Performance is not an issue, as the <code>rowcount</code> is fairly small and it's only manually run when needed.<br>
  ...or just tell me if I'm doing it all wrong.</p>