2013-04-29 94 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