问题
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
)
回答1:
You forgot to add alias
at 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 Into
like this:
INSERT into foo (CalMonth,CalYear,InvoiceDate,StockCode,QtyInvoiced,Volume)
SELECT * 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
) AS table1
For example see this fiddle
2. If you have not created a table then you can do this using SELECT * INTO
like this:
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
) AS table1
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
回答2:
Try this
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
) as TAB
Just provide an alias
to your sub queried table
回答3:
Try this:
INSERT into foo (CalMonth,CalYear, InvoiceDate, StockCode, QtyInvoiced, Volume)
Select * 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
) as t
回答4:
I think that you either need to add the alias that has been suggested or (more complex) to remove the parenthesis, the *
, the FROM
, the second SELECT
and move the INTO foo
:
SELECT
--- removed: FROM
--- removed: (
--- removed: SELECT
DATEPART(MONTH,a.InvoiceDate) as CalMonth
,DATEPART(YEAR,a.InvoiceDate) as CalYear
,...
INTO foo --- moved
FROM sales a
UNION ALL
SELECT ds.CalMonth as CalMonth
,...
FROM sales1 ds ;
--- removed: )
来源:https://stackoverflow.com/questions/11882610/create-new-table-with-subquery-select