我正在使用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
)
但它給了我錯誤。我如何在表格中插入記錄?謝謝。
「它給我錯誤」 - 什麼錯誤? – 2013-04-29 08:19:47