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

4 回答 4

4

您忘记alias在查询末尾添加。

您可以通过两种方法做到这一点:

1.如果您已经创建了一个表,那么您可以这样做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

例如看到这个小提琴

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

例如看到这个小提琴

有关更多参考,请参阅SQL SERVER – 将数据从一个表插入到另一个表 – INSERT INTO SELECT – SELECT INTO TABLE

于 2012-08-09T11:52:06.950 回答
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向您的子查询表提供一个

于 2012-08-09T11:43:39.910 回答
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
于 2012-08-09T11:44:03.213 回答
0

我认为您要么需要添加已建议的别名,要么(更复杂)删除括号、 the *、 the FROM、第二个SELECT并移动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: )
于 2012-08-09T12:00:30.487 回答