5

我正在使用 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
    )

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

4

7 回答 7

11

试试这个——

;WITH CTE AS 
(
    SELECT   ...
    FROM     @tmp
)
INSERT INTO dbo.tbl (....)
SELECT ..
FROM CTE
GROUP BY ...
ORDER BY ... 
于 2013-04-29T08:09:19.507 回答
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
于 2016-08-26T15:37:08.423 回答
2

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

所以发帖

另一个例子:

CTE插入

这可能会或可能不会帮助您处理 SP,但您始终可以尝试使用表值函数返回数据:

表值函数

于 2013-04-29T08:06:15.067 回答
0

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

它可能有效

于 2014-01-21T09:55:31.963 回答
0

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

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
于 2014-02-28T06:45:35.117 回答
0

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

检查这篇文章。http://adroitjam.com/cte-common-table-expression-insert-update-delete/

于 2013-10-31T03:42:28.703 回答
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
于 2013-04-29T08:12:05.453 回答