title: T-SQL, Common Table Expression to a temp table, then TRUNCATE, and INSERT from temp table. Reasonable practice?
body: <p>I have a stored procedure that via a series of CTEs builds a result set which is then piped into a temp table.</p>
  <p>It then <code>TRUNCATE</code>s the existing table, and <code>INSERT</code>s the result set from the temp table.</p>
  <p>I'm usually only on querying side, not the stored procedure. Just wondering if this is a reasonable way to drop &amp; rebuild a table.</p>
  <pre><code>IF OBJECT_ID('tempdb..#factTSPOrderGoals') IS NOT NULL  
      DROP TABLE #factTSPOrderGoals;
  ....
  
  &lt;Bunch of CTEs&gt;
  ...
  
  SELECT * 
  INTO #factTSPOrderGoals 
  FROM CTE_Final
  
  BEGIN TRANSACTION 
      TRUNCATE TABLE dbo.factTSPOrderGoals
  
      INSERT INTO dbo.factTSPOrderGoals 
          SELECT * 
          FROM #factTSPOrderGoals 
  
  COMMIT TRANSACTION;
  </code></pre>
  <p>Any SQL command clears all CTEs - thus that intermediate step of writing to a temp table. Feels both clean &amp; kinda janky, so curious how 'reasonable' this is in general.</p>