SELECT INTO with subquery

Kenny picture Kenny · Sep 15, 2015 · Viewed 7.6k times · Source

Regarding SELECT INTO in SQL Server

The following throw an error Incorrect syntax near ')'.

SELECT * INTO Sales.MyTable FROM 
(SELECT TOP(100) * FROM Sales.Customer)

The following will pass

With tempCust AS
(
SELECT TOP(100) * FROM Sales.Customer
)
SELECT * INTO Sales.MyTable FROM tempCust

What is the rule behind that ?

Answer

Amnesh Goel picture Amnesh Goel · Sep 15, 2015

Can you add an alias to your subquery like shown below and then give it a try..

SELECT * INTO Sales.MyTable FROM 
(SELECT TOP(100) * FROM Sales.Customer) as abc