当我在SQL Server中执行SELECT..INSERT时,为什么会出现此错误

时间:2014-09-03 08:02:23

标签: sql sql-server tsql

为什么我会收到此错误

  

The select list for the INSERT statement contains more items than the insert list. The number of SELECT values must match the number of INSERT columns.

当我运行此查询时

INSERT INTO TempOutputOfGroupifySP
    (MonthOfQuery,Associate,[NoOfClaims],[ActualNoOfLines],[AverageTATInDays],
    [NoOfErrorsDiscovered],[VarianceinPercent],[NoOfClaimsAudited],[InternalQualInPercent],[ExternalQualInPercent]
    )

SELECT (DATENAME(MONTH,[ClaimProcessedDate])) AS MonthOfQuery,
    Temp.Associate AS Associate,
    COUNT(*) AS [NoOfClaims],
    SUM(NoOfLines) AS [ActualNoOfLines] ,
    (SUM(DATEDIFF(dd,[ClaimReceivedDate],[ClaimProcessedDate]))/COUNT(*))  AS [AverageTATInDays],
    A.[NoOfErrorsDiscovered] AS [NoOfErrorsDiscovered],
    Temp.[MonthlyTarget] As [TargetNoOfLines],(Temp.[MonthlyTarget] - COUNT(*)) AS [VarianceInPercent],
    B.[NoOfClaimsAudited] AS [NoOfClaimsAudited],
    ((A.[NoOfErrorsDiscovered]/NULLIF(B.[NoOfClaimsAudited],0))*100) AS [InternalQualInPercent],
    NULL AS [ExternalQualInPercent]
    FROM 
    (SELECT COUNT(*) AS [NoOfErrorsDiscovered] FROM TempTableForStatisticsOfAssociates T1 WHERE [TypeOfError] IS NOT NULL) AS A, 
    (SELECT COUNT(*) AS [NoOfClaimsAudited] FROM TempTableForStatisticsOfAssociates T2 WHERE Auditor IS NOT NULL) AS B,
    TempTableForStatisticsOfAssociates Temp
    GROUP BY DATENAME(MONTH,([ClaimProcessedDate])),
    Temp.Associate,
    A.[NoOfErrorsDiscovered],
    Temp.[MonthlyTarget],
    B.[NoOfClaimsAudited]

目标表的结构是

CREATE TABLE [dbo].[TempOutputOfGroupifySP](
    [MonthOfQuery] [nchar](10) NULL,
    [Associate] [nvarchar](max) NULL,
    [NoOfClaims] [int] NULL,
    [ActualNoOfLines] [int] NULL,
    [AverageTATInDays] [int] NULL,
    [NoOfErrorsDiscovered] [int] NULL,
    [VarianceInPercent] [float] NULL,
    [NoOfClaimsAudited] [int] NULL,
    [InternalQualInPercent] [float] NULL,
    [ExternalQualInPercent] [float] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

1 个答案:

答案 0 :(得分:2)

您的INSERT INTO为插入定义了10个列,但是,SELECT语句返回11列。您要么错过INSERT语句中的列,要么在SELECT语句中返回太多列。 比较您的表格结构以及SELECTINSERT SELECT语句中的以下行并不是对应的:

Temp.[MonthlyTarget] As [TargetNoOfLines]