title: How to store query result into temporary table
body: <p>I am trying to insert the results generated from SQL query into temporary table using following query:</p>
  
  <pre><code>SELECT * 
  INTO #YourTempTable
  FROM
      (WITH cte AS 
       (
          SELECT
              sb.case, sb.Trace, sb.Amount, sp.edge, sp.UserId,
              count(*) over (partition by sp.edge) as cnt
          FROM 
              Budget sb 
          JOIN
              Sap sp ON sb.Trace = sp.Trace
          WHERE 
              sb.Trace IN (SELECT Trace FROM Sap 
                           WHERE sb.UserId in ('R5', 'HB'))
       )
       SELECT cte.*
       FROM cte
       WHERE cnt &gt; 1
       ORDER BY edge DESC;
  )
  </code></pre>
  
  <p>I'm getting error: </p>
  
  <blockquote>
    <p>Msg 156, Level 15, State 1, Line 4<br>
    Incorrect syntax near the keyword 'with'.</p>
    
    <p>Msg 319, Level 15, State 1, Line 4<br>
    Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.</p>
    
    <p>Msg 102, Level 15, State 1, Line 12<br>
    Incorrect syntax near ')'.</p>
  </blockquote>
  
  <p>Can anyone please let me know about this?</p>