I’m trying to create a new table from a subquery select but I get the following error:
Incorrect syntax near ‘)’.
SELECT * INTO foo
FROM
(
SELECT DATEPART(MONTH,a.InvoiceDate) as CalMonth
,DATEPART(YEAR,a.InvoiceDate) as CalYear
,a.InvoiceDate
,a.StockCode
,a.QtyInvoiced
,a.Volume
FROM sales a
UNION ALL
SELECT ds.CalMonth as CalMonth
,ds.CalYear as CalYear
,ds.InvoiceDate
,ds.StockCode
,ds.Cases as QtyInvoiced
,ds.Vol as Volume
FROM sales1 ds
)
You forgot to add
aliasat the end of your query.You can do this by two methods:
1. If you have already created a table then you can do this using
Insert Intolike this:For example see this fiddle
2. If you have not created a table then you can do this using
SELECT * INTOlike this:For example see this fiddle
For more reference see SQL SERVER – Insert Data From One Table to Another Table – INSERT INTO SELECT – SELECT INTO TABLE