2012-08-09 96 views
4

我試圖創建一個子查詢中選擇一個新表,但我得到以下錯誤:
附近有語法錯誤)「。創建子查詢新表中選擇

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 
) 
+3

其RDBMSÿ你正在使用。究竟是什麼錯誤? – sundar 2012-08-09 11:43:24

+0

在大多數DBMS中,使用'CREATE TABLE foo AS select ...'創建一個新表,也許您需要使用該語法。 – 2012-08-09 11:56:55

回答

3

你忘了你查詢的末尾添加alias

您可以通過兩種方法做到這一點:

如果您已經創建了一個表,那麼你可以做使用Insert Into這是這樣的:

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 

例如see this fiddle

2.如果您尚未創建一個表,那麼你可以做使用SELECT * INTO這是這樣的:

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 

例如see this fiddle

有關詳細信息參見SQL SERVER – Insert Data From One Table to Another Table – INSERT INTO SELECT – SELECT INTO TABLE

3

試試這個

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 

只需提供一個alias你的子查詢的表

0

試試這個:

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 
0

我認爲你要麼需要補充的是已提出的別名或(更復雜),以去除括號中,*,的FROM,第二SELECT並移動​​3210:

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:)