4

サブクエリselectから新しいテーブルを作成しようとしていますが、次のエラーが発生します:
')'の近くの構文が正しくありません。

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クエリの最後 に追加するのを忘れました。

これは、次の 2 つの方法で行うことができます。

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

*提案されたエイリアスを追加するか、(より複雑に) かっこを削除する必要がある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 に答える