首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在SQL 2012中将选择数据插入临时表

在SQL 2012中将选择数据插入临时表
EN

Stack Overflow用户
提问于 2019-03-07 06:26:58
回答 2查看 845关注 0票数 0

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

代码语言:javascript
复制
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

我尝试过遵循解决方案

代码语言:javascript
复制
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)

我跟踪了错误

代码语言:javascript
复制
Incorrect syntax near ')'.

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-03-07 06:36:13

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

代码语言:javascript
复制
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插入到临时表中。

代码语言:javascript
复制
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
票数 2
EN

Stack Overflow用户

发布于 2019-03-07 06:48:50

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

代码语言:javascript
复制
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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55037348

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档