在SQL 2012中将Select数据插入临时表

在SQL 2012中将Select数据插入临时表,sql,sql-server,Sql,Sql Server,问题:如何在sql 2012中将复杂的select数据插入临时表 select ROW_NUMBER() OVER(order by ppt.type) as Item_code, ppt.type type, ..., ..., ..., '11/02/19 09:51' Created_dt from product psi inner join [DB1]..items ios on ios.icode=psi.icode inner join [DB2]..types ppt on p

问题:如何在sql 2012中将复杂的select数据插入临时表

select ROW_NUMBER() OVER(order by ppt.type) as Item_code,
ppt.type type,
...,
...,
...,
'11/02/19 09:51' Created_dt
from product psi
inner join  [DB1]..items ios on ios.icode=psi.icode
inner join [DB2]..types ppt on ppt.type=ios.type
我尝试了以下解决方案

select * into #temptable from
(select ROW_NUMBER() OVER(order by ppt.type) as Item_code,
ppt.type type,
...,
...,
...,
'11/02/19 09:51' Created_dt
from product psi
inner join  [DB1]..items ios on ios.icode=psi.icode
inner join [DB2]..types ppt on ppt.type=ios.type)
我发现以下错误

Incorrect syntax near ')'.

当我正常运行select语句时,我会得到预期的数据

以下代码的语法是正确的:

SELECT *
INTO #Temptable
FROM
(
    SELECT ROW_NUMBER() OVER(ORDER BY Ppt.Type) AS Item_Code,
           Ppt.Type AS Type,
           '11/02/19 09:51' AS Created_Dt
    FROM Product AS Psi
      INNER JOIN Db1..Items AS Ios ON Ios.Icode = Psi.Icode
      INNER JOIN Db2..Types AS Ppt ON Ppt.Type = Ios.Type );
通常,您可以捕获CTE中的逻辑并将CTE插入temp表中

USE SomeDB;
WITH CTE AS 
(

    SELECT *
    FROM
    (
       SELECT ROW_NUMBER() OVER(ORDER BY Ppt.Type) AS Item_Code,
            Ppt.Type AS Type,
            '11/02/19 09:51' AS Created_Dt
       FROM Product AS Psi
        INNER JOIN Db1..Items AS Ios ON Ios.Icode = Psi.Icode
        INNER JOIN Db2..Types AS Ppt ON Ppt.Type = Ios.Type )
)

INSERT INTO #T
SELECT * FROM CTE

就语法而言,以下代码是正确的:

SELECT *
INTO #Temptable
FROM
(
    SELECT ROW_NUMBER() OVER(ORDER BY Ppt.Type) AS Item_Code,
           Ppt.Type AS Type,
           '11/02/19 09:51' AS Created_Dt
    FROM Product AS Psi
      INNER JOIN Db1..Items AS Ios ON Ios.Icode = Psi.Icode
      INNER JOIN Db2..Types AS Ppt ON Ppt.Type = Ios.Type );
通常,您可以捕获CTE中的逻辑并将CTE插入temp表中

USE SomeDB;
WITH CTE AS 
(

    SELECT *
    FROM
    (
       SELECT ROW_NUMBER() OVER(ORDER BY Ppt.Type) AS Item_Code,
            Ppt.Type AS Type,
            '11/02/19 09:51' AS Created_Dt
       FROM Product AS Psi
        INNER JOIN Db1..Items AS Ios ON Ios.Icode = Psi.Icode
        INNER JOIN Db2..Types AS Ppt ON Ppt.Type = Ios.Type )
)

INSERT INTO #T
SELECT * FROM CTE

问题是,您正在将未命名数据源中的数据插入到表中

select * into #temptable from
(select ROW_NUMBER() OVER(order by ppt.type) as Item_code,
ppt.type type,
...,
...,
...,
'11/02/19 09:51' Created_dt
from product psi
inner join  [DB1]..items ios on ios.icode=psi.icode
inner join [DB2]..types ppt on ppt.type=ios.type) as tbl

只要这样做,你的问题就会解决。我刚刚为您插入数据的源添加了一个别名。

问题是,您正在将未命名数据源中的数据插入到表中

select * into #temptable from
(select ROW_NUMBER() OVER(order by ppt.type) as Item_code,
ppt.type type,
...,
...,
...,
'11/02/19 09:51' Created_dt
from product psi
inner join  [DB1]..items ios on ios.icode=psi.icode
inner join [DB2]..types ppt on ppt.type=ios.type) as tbl

只要这样做,你的问题就会解决。我刚刚为您插入数据的源添加了一个别名。

Created_dt,(逗号不应该在这里)在真正的查询中,我没有再次编辑过逗号检查,这是“修复我的代码,但我不会向您显示我的代码”?@Damien_the_Unsivers whyWell,到目前为止,有人发现代码有一个问题,但它与“假查询”有关在这个问题上,不是你有实际问题的“真正的查询”。在你的第二个查询的末尾有一个明显的额外的
在第一个查询中没有出现,但是谁知道这是否与你的实际问题有关…创建了\u dt,(逗号不应该在这里)在真实的查询中没有逗号我再次编辑了检查,这是“修复我的代码,但我不会向你展示我的代码”?@Damien_The_unsiver whyWell,到目前为止,有人发现了代码的一个问题,但问题中的“假查询”,而不是你实际遇到问题的“真实查询”。在第二个查询的末尾有一个明显的额外的
,在第一个查询中没有出现,但是谁知道这是否与您的实际问题有关…感谢使用公共表表达式对我有效感谢使用公共表表达式对我有效