2013-04-29 93 views
4

我正在使用SQL Server 2008.为了获取某些行,我在存储过程中使用了CTE。从CTE插入记录到表

;WITH 
CTE AS (
    SELECT BrokerId , 
        RankId , 
        BrokerName , 
        RankName , 
        BrokerCode , 
        IntroducerCode , 
        CscName , 
        MAX(SIP) AS SIP , 
        MAX(Fresh) AS Fresh , 
        MAX(FY) AS FY , 
        MAX(SY) AS SY , 
        MAX(TY) AS TY , 
        CscId , 
        Promotive , 
        NoOfPromotive , 
        PlanTypeName , 
        PlanYear 
    FROM @tmp 
    GROUP BY BrokerId , 
        RankId , 
        BrokerName , 
        RankName , 
        BrokerCode , 
        IntroducerCode , 
        CscName , 
        CscId , 
        Promotive , 
        NoOfPromotive , 
        PlanTypeName , 
        PlanYear 
) 
SELECT BrokerId , 
     RankId , 
     BrokerName , 
     RankName , 
     BrokerCode , 
     IntroducerCode , 
     CscName , 
     SUM(SIP) AS 'SIP' , 
     SUM(Fresh) AS 'Fresh' , 
     SUM(FY) AS 'FY' , 
     SUM(SY) AS 'SY' , 
     SUM(TY) AS 'TY' , 
     Promotive , 
     Total = ISNULL((SUM(SIP)), 0) + ISNULL((SUM(Fresh)), 0) 
     + ISNULL((SUM(FY)), 0) + ISNULL((SUM(SY)), 0) 
     + ISNULL((SUM(TY)), 0) , 
     NoOfPromotive , 
     PlanTypeName , 
     PlanYear , 
     CscId 
FROM CTE 
GROUP BY BrokerId , 
     RankId , 
     BrokerName , 
     RankName , 
     BrokerCode , 
     IntroducerCode , 
     CscName , 
     Promotive , 
     NoOfPromotive , 
     PlanTypeName , 
     PlanYear , 
     CscId 
ORDER BY PlanTypeName 

它给了我正确的数据。现在我想将这些数据插入到表格中。我试过像:

INSERT INTO MyTable 
    (BrokerId , 
     RankId , 
     BrokerName , 
     RankName , 
     BrokerCode , 
     IntroducerCode , 
     CscName , 
     SIP , 
     Fresh , 
     FY , 
     SY , 
     TY , 
     Promotive , 
     Total , 
     NoOfPromotive , 
     PlanTypeName , 
     PlanYear , 
     CscId 

    ) 
    (SELECT BrokerId , 
       RankId , 
       BrokerName , 
       RankName , 
       BrokerCode , 
       IntroducerCode , 
       CscName , 
       SUM(SIP) AS 'SIP' , 
       SUM(Fresh) AS 'Fresh' , 
       SUM(FY) AS 'FY' , 
       SUM(SY) AS 'SY' , 
       SUM(TY) AS 'TY' , 
       Promotive , 
       Total = ISNULL((SUM(SIP)), 0) + ISNULL((SUM(Fresh)), 0) 
       + ISNULL((SUM(FY)), 0) + ISNULL((SUM(SY)), 0) 
       + ISNULL((SUM(TY)), 0) , 
       NoOfPromotive , 
       PlanTypeName , 
       PlanYear , 
       CscId 
     FROM  CTE 
     GROUP BY BrokerId , 
       RankId , 
       BrokerName , 
       RankName , 
       BrokerCode , 
       IntroducerCode , 
       CscName , 
       Promotive , 
       NoOfPromotive , 
       PlanTypeName , 
       PlanYear , 
       CscId 
    ) 

但它给了我错误。我如何在表格中插入记录?谢谢。

+1

“它给我错误” - 什么错误? – 2013-04-29 08:19:47

回答

2

您可以直接从CTE插入表中,这里是一个这样的例子:

SO post

和另一个例子:

CTE Insert

这可能会或可能不会帮助你与SP,但你总是可以尝试一个表值函数来返回数据:

Table valued function

10

尝试这一个 -

;WITH CTE AS 
(
    SELECT ... 
    FROM  @tmp 
) 
INSERT INTO dbo.tbl (....) 
SELECT .. 
FROM CTE 
GROUP BY ... 
ORDER BY ... 
+0

Doh。当你看到它时非常明显。 – Swanny 2015-07-20 04:15:16

0

CTE尽可能快,因为它是在查询中使用破坏。 在WITH CTE AS()查询之后,您正在执行返回数据的SELECT查询。但在此之后,CTE不能用于INSERT查询。

您需要在制定CTE后立即插入。

这是MSDN

A common table expression (CTE) can be thought of as a temporary result set that is defined 
within the execution scope of a single SELECT, INSERT, UPDATE, DELETE, or CREATE VIEW 
statement. A CTE is similar to a derived table in that it is not stored as an object and lasts 
only for the duration of the query. 

因此,这将正常工作。

WITH CTE 
     AS (SELECT BrokerId , 
        RankId , 
        BrokerName , 
        RankName , 
        BrokerCode , 
        IntroducerCode , 
        CscName , 
        MAX(SIP) AS SIP , 
        MAX(Fresh) AS Fresh , 
        MAX(FY) AS FY , 
        MAX(SY) AS SY , 
        MAX(TY) AS TY , 
        CscId , 
        Promotive , 
        NoOfPromotive , 
        PlanTypeName , 
        PlanYear 
      FROM  @tmp 
      GROUP BY BrokerId , 
        RankId , 
        BrokerName , 
        RankName , 
        BrokerCode , 
        IntroducerCode , 
        CscName , 
        CscId , 
        Promotive , 
        NoOfPromotive , 
        PlanTypeName , 
        PlanYear 
     ) 

待办事项INSERT立即现在

INSERT INTO MyTable 
    (BrokerId , 
     RankId , 
     BrokerName , 
     RankName , 
     BrokerCode , 
     IntroducerCode , 
     CscName , 
     SIP , 
     Fresh , 
     FY , 
     SY , 
     TY , 
     Promotive , 
     Total , 
     NoOfPromotive , 
     PlanTypeName , 
     PlanYear , 
     CscId 

    ) 
    (SELECT BrokerId , 
       RankId , 
       BrokerName , 
       RankName , 
       BrokerCode , 
       IntroducerCode , 
       CscName , 
       SUM(SIP) AS 'SIP' , 
       SUM(Fresh) AS 'Fresh' , 
       SUM(FY) AS 'FY' , 
       SUM(SY) AS 'SY' , 
       SUM(TY) AS 'TY' , 
       Promotive , 
       Total = ISNULL((SUM(SIP)), 0) + ISNULL((SUM(Fresh)), 0) 
       + ISNULL((SUM(FY)), 0) + ISNULL((SUM(SY)), 0) 
       + ISNULL((SUM(TY)), 0) , 
       NoOfPromotive , 
       PlanTypeName , 
       PlanYear , 
       CscId 
     FROM  CTE 
     GROUP BY BrokerId , 
       RankId , 
       BrokerName , 
       RankName , 
       BrokerCode , 
       IntroducerCode , 
       CscName , 
       Promotive , 
       NoOfPromotive , 
       PlanTypeName , 
       PlanYear , 
       CscId 
    ) 

现在你可以从MyTable的

Select * from MyTable 
0

请插入查询后移除选择查询括号和执行完整的代码在一次

它可能工作

0

您共同表达TABLE语句之前,你的选择常见的表现数据之前声明的临时表在将数据选择到之前创建的临时表之前,先放入一条插入语句。

DECLARE @MyTable TABLE 
    (BrokerId int,RankId int,BrokerName varchar,RankName varchar, BrokerCode varchar, IntroducerCode varchar, CscName varchar,SIP int,Fresh int 
    ,FY int,SY int,TY int,Promotive int,Total int,NoOfPromotive int,PlanTypeName int,PlanYear int,CscId int) 

WITH [CTE] AS(
    SELECT BrokerId , 
       RankId , 
       BrokerName , 
       RankName , 
       BrokerCode , 
       IntroducerCode , 
       CscName , 
       SUM(SIP) AS 'SIP' , 
       SUM(Fresh) AS 'Fresh' , 
       SUM(FY) AS 'FY' , 
       SUM(SY) AS 'SY' , 
       SUM(TY) AS 'TY' , 
       Promotive , 
       Total = ISNULL((SUM(SIP)), 0) + ISNULL((SUM(Fresh)), 0) 
       + ISNULL((SUM(FY)), 0) + ISNULL((SUM(SY)), 0) 
       + ISNULL((SUM(TY)), 0) , 
       NoOfPromotive , 
       PlanTypeName , 
       PlanYear , 
       CscId 
     FROM  CTE 
     GROUP BY BrokerId , 
       RankId , 
       BrokerName , 
       RankName , 
       BrokerCode , 
       IntroducerCode , 
       CscName , 
       Promotive , 
       NoOfPromotive , 
       PlanTypeName , 
       PlanYear , 
       CscId 
) 
INSERT INTO @MyTable 
SELECT * FROM [CTE] 
SELECT * FROM @MyTable 
3

注意:这已被正确答复为SQL-Server。但是,如果你偶然发现这篇文章,寻找相同的答案,但你使用了其他一些DBMS(即SYBASE,ORACLE或其他),这是行不通的。您不能在CTE之后立即使用INSERT语句。在这些情况下,请尝试先插入语句:

INSERT INTO someTable (Col1,Col2,Col3) 
WITH CTE AS (
SELECT someColA, 
     someColB, 
     someColC 
FROM anotherTable 
) 
SELECT someColA, 
     someColB, 
     someColC 
FROM CTE