Creating Temp Table with SELECT * INTO from CTE

How to create a temp table using SELECT * INTO from a CTE query?

In SQL, you can create a temporary table using SELECT * INTO from a CTE (Common Table Expression) by first defining the CTE and then selecting from it.

Here’s the general pattern: – Step 1: Define a CTE WITH MyCTE AS ( SELECT column1, column2 FROM YourTable WHERE SomeCondition = 1 )

– Step 2: Create a temp table from the CTE SELECT * INTO #TempTable FROM MyCTE;

– Now you can use the temp table SELECT * FROM #TempTable;

1 Like