使用子查询选择创建新表

时间:2012-08-09 11:39:58

标签: sql subquery

我正在尝试从子查询选择中创建一个新表,但是我收到以下错误:左')'附近的语法不正确。

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 个答案:

答案 0 :(得分: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

答案 1 :(得分:3)

您忘记在查询结尾添加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

例如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

答案 2 :(得分: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

答案 3 :(得分:0)

我认为您需要添加已建议的别名或(更复杂)删除括号,*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: )