将记录从CTE插入表格

时间:2013-04-29 08:04:00

标签: sql-server sql-server-2008

我正在使用SQL Server 2008.为了获取一些行,我在我的存储过程中使用了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
    )

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

7 个答案:

答案 0 :(得分:10)

试试这个 -

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

答案 1 :(得分:3)

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

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

答案 2 :(得分:2)

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

SO post

和另一个例子:

CTE Insert

这可能会或可能不会帮助您使用SP,但您总是可以尝试使用表值函数来返回数据:

Table valued function

答案 3 :(得分:0)

CTE在查询中使用后立即销毁。 在WITH CTE AS()查询之后,您正在执行返回数据的SELECT查询。但是之后,INSERT查询无法使用CTE。

您需要在制定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

答案 4 :(得分:0)

您不能对包含聚合函数的CTE执行DML操作。

查看此帖子。 http://adroitjam.com/cte-common-table-expression-insert-update-delete/

答案 5 :(得分:0)

请在插入查询后删除选择查询的大括号并立即执行完整代码

它可能有用

答案 6 :(得分:0)

在公共表达式表语句之前声明一个临时表,在选择公共表达式表中的数据之前,在将数据选择到之前创建的临时表之前,先插入一个insert语句。

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