SQL Server INSERT ... SELECT语句不会解析

时间:2010-06-10 20:29:36

标签: sql sql-server sql-server-2005

我收到SQL Server 2005的以下错误消息

  

Msg 120,Level 15,State 1,Procedure usp_AttributeActivitiesForDateRange,Line 18   INSERT语句的选择列表包含的项目少于插入列表。 SELECT值的数量必须与INSERT列的数量匹配。

我已复制并粘贴选择列表并将列表插入Excel,并验证每个列表中的项目数相同。两个表中的附加主键字段未在insert语句或选择列表中列出。我不确定这是否相关,但可能是可疑的。这是我的存储过程的源代码:

CREATE PROCEDURE [dbo].[usp_AttributeActivitiesForDateRange]
(
    @dtmFrom DATETIME,
    @dtmTo DATETIME
)
AS
BEGIN
    SET NOCOUNT ON;

    DECLARE @dtmToWithTime DATETIME

    SET @dtmToWithTime = DATEADD(hh, 23, DATEADD(mi, 59, DATEADD(s, 59, @dtmTo)));

    -- Get uncontested DC activities

    INSERT INTO AttributedDoubleClickActivities
        ([Time],
        [User-ID],
        [IP],
        [Advertiser-ID],
        [Buy-ID],
        [Ad-ID],
        [Ad-Jumpto],
        [Creative-ID],
        [Creative-Version],
        [Creative-Size-ID],
        [Site-ID],
        [Page-ID],
        [Country-ID],
        [State Province],
        [Areacode],
        [OS-ID],
        [Domain-ID],
        [Keyword],
        [Local-User-ID],
        [Activity-Type],
        [Activity-Sub-Type],
        [Quantity],
        [Revenue],
        [Transaction-ID],
        [Other-Data],
        Ordinal,
        [Click-Time],
        [Event-ID]) SELECT 
            [Time],
            [User-ID],
            [IP],
            [Advertiser-ID],
            [Buy-ID],
            [Ad-ID],
            [Ad-Jumpto],
            [Creative-ID],
            [Creative-Version],
            [Creative-Size-ID],
            [Site-ID],
            [Page-ID],
            [Country-ID],
            [State Province],
            [Areacode],
            [OS-ID],
            [Domain-ID],
            [Keyword],
            [Local-User-ID]
            [Activity-Type],
            [Activity-Sub-Type],
            [Quantity],
            [Revenue],
            [Transaction-ID],
            [Other-Data],
            REPLACE(Ordinal, '?', '') AS Ordinal,
            [Click-Time],
            [Event-ID]
        FROM Activity_Reports
            WHERE [Time] BETWEEN @dtmFrom AND @dtmTo
            AND REPLACE(Ordinal, '?', '') IN 
        (SELECT REPLACE(Ordinal, '?', '') FROM Activity_Reports 
            WHERE [Time] BETWEEN @dtmFrom AND @dtmTo
        EXCEPT
        SELECT CONVERT(VARCHAR, TripID) FROM VisualSciencesActivities
            WHERE [Time] BETWEEN @dtmFrom AND @dtmTo);

END
GO

5 个答案:

答案 0 :(得分:14)

您在[Local-User-ID][Activity-Type]之间缺少逗号。

尝试:

INSERT INTO attributeddoubleclickactivities 
            ([Time], 
             [User-ID], 
             [IP], 
             [Advertiser-ID], 
             [Buy-ID], 
             [Ad-ID], 
             [Ad-Jumpto], 
             [Creative-ID], 
             [Creative-Version], 
             [Creative-Size-ID], 
             [Site-ID], 
             [Page-ID], 
             [Country-ID], 
             [State Province], 
             [Areacode], 
             [OS-ID], 
             [Domain-ID], 
             [Keyword], 
             [Local-User-ID], 
             [Activity-Type], 
             [Activity-Sub-Type], 
             [Quantity], 
             [Revenue], 
             [Transaction-ID], 
             [Other-Data], 
             ordinal, 
             [Click-Time], 
             [Event-ID]) 
SELECT [Time], 
       [User-ID], 
       [IP], 
       [Advertiser-ID], 
       [Buy-ID], 
       [Ad-ID], 
       [Ad-Jumpto], 
       [Creative-ID], 
       [Creative-Version], 
       [Creative-Size-ID], 
       [Site-ID], 
       [Page-ID], 
       [Country-ID], 
       [State Province], 
       [Areacode], 
       [OS-ID], 
       [Domain-ID], 
       [Keyword], 
       [Local-User-ID],
       [Activity-Type], 
       [Activity-Sub-Type], 
       [Quantity], 
       [Revenue], 
       [Transaction-ID], 
       [Other-Data], 
       REPLACE(ordinal, '?', '') AS ordinal, 
       [Click-Time], 
       [Event-ID] 
FROM   activity_reports 
WHERE  [Time] BETWEEN @dtmFrom AND @dtmTo 
       AND REPLACE(ordinal, '?', '') IN (SELECT REPLACE(ordinal, '?', '') 
                                         FROM   activity_reports 
                                         WHERE  [Time] BETWEEN 
                                                @dtmFrom AND @dtmTo 
                                         EXCEPT 
                                         SELECT CONVERT(VARCHAR, tripid) 
                                         FROM   visualsciencesactivities 
                                         WHERE  [Time] BETWEEN 
                                                @dtmFrom AND @dtmTo); 

答案 1 :(得分:4)

你在这里错过了一个逗号:

[Local-User-ID] [Activity-Type]

应该是:

[Local-User-ID], [Activity-Type]

答案 2 :(得分:1)

您在[Local-User-ID]之后忘记了一个逗号,因此它将该列别名为[Activity-Type]

常见错误

本质上你有本地用户ID] AS [活动类型],AS是可选的

答案 3 :(得分:0)

您的选择列表中没有足够的字段用于您提供的插入语句。

答案 4 :(得分:0)

在选择中有一个拼写错误

[Local-User-ID]
            [Activity-Type],

你错过了“,”!