在SQL中使用select语句创建表时出错

时间:2018-10-03 14:53:56

标签: sql sql-server ssms

我正在尝试使用select语句创建表,但出现以下错误。

注意:我在没有创建的情况下运行了select语句,它起作用了

CREATE TABLE shipments_temp AS 
  (SELECT Concat('W', RIGHT([calendar year week], 2), '-', 
                      LEFT([calendar year week], 4)), 
          [market], 
          [base product code], 
          Sum([sell-in history]) AS Shipments 
   FROM   apac_092016_092018_shipments 
   GROUP  BY Concat('W', RIGHT([calendar year week], 2), '-', 
                         LEFT([calendar year week], 4)), 
             [market], 
             [base product code]);

enter image description here

1 个答案:

答案 0 :(得分:7)

SQL Server不支持create table as。而是使用into

select concat('W', right([Calendar Year Week],2), '-', LEFT([Calendar Year Week], 4)
             ) as column_whatever, -- you need a column alias
      [Market], [Base Product Code], sum([Sell-in History]) as Shipments
into Shipments_Temp
from APAC_092016_092018_Shipments
Group by concat('W', right([Calendar Year Week], 2), '-', LEFT([Calendar Year Week], 4)),
         [Market], [Base Product Code]);

您还需要命名目标表的第一列。