1

大家早上好

我的老板帮助我设计了一个查询,其中填充了 137 万行随机数据,他现在要求我将结果插入/更新到空白表中。但由于某种原因,我无法让它工作。

三列是 ArrivalDate、PitchType_Skey 和 Site_Skey。但是当我运行我的查询(见下文)时,我收到一条错误消息,我不知道为什么。你能帮我吗?

消息 121,级别 15,状态 1,第 2 行
INSERT 语句的选择列表包含的项目多于插入列表。SELECT 值的数量必须与 INSERT 列的数量相匹配。

询问:

USE Occupancy

INSERT INTO Bookings (ArrivalDate, Site_Skey, PitchType_Skey)
    SELECT        
       Time.Date, Site.Site_Skey, Site.SiteWeighting, PitchType.PitchType_Skey, 
       PitchType.PitchTypeWeighting, 
       RAND(checksum(NEWID())) * Site.SiteWeighting * PitchType.PitchTypeWeighting AS Expr1
    FROM            
       Capacity 
    INNER JOIN
       Site ON Capacity.Site_Skey = Site.Site_Skey 
    INNER JOIN
       PitchType ON Capacity.PitchType_Skey = PitchType.PitchType_Skey 
    INNER JOIN
       Time 
    INNER JOIN
       AGKey ON Time.ArrivalDayWeighting = AGKey.[Key] ON Capacity.StartDate <= Time.Date AND Capacity.EndDate >= Time.Date 
    CROSS JOIN
        (SELECT 0 AS col1
         UNION ALL
         SELECT 1 AS col1) AS aaav
    WHERE        
       (Time.CalendarYear = 2010) 
       AND (RAND(checksum(NEWID())) * Site.SiteWeighting * PitchType.PitchTypeWeighting >= 1.22)

谢谢

韦恩

4

2 回答 2

2

错误信息给你答案。您的SELECT列表中有更多项目 (6)

Time.Date
Site.Site_Skey
Site.SiteWeighting
PitchType.PitchType_Skey
PitchType.PitchTypeWeighting
RAND(checksum(NEWID())) * Site.SiteWeighting * PitchType.PitchTypeWeighting AS Expr1

比您在INSERT列表中所做的 (3)

ArrivalDate
Site_Skey
PitchType_Skey

从您的SELECT列表中删除一些列或将一些列添加到您的INSERT列表中。

由于您没有给出Bookings表格的完整结构,我只能猜测您需要这样做

USE Occupancy
INSERT INTO Bookings
(
    ArrivalDate,
    Site_Skey,
    PitchType_Skey
)
SELECT
    Time.Date,
    Site.Site_Skey,
    PitchType.PitchType_Skey
FROM
    Capacity
    INNER JOIN Site ON Capacity.Site_Skey = Site.Site_Skey
    INNER JOIN PitchType ON Capacity.PitchType_Skey = PitchType.PitchType_Skey
    INNER JOIN Time
    INNER JOIN AGKey ON Time.ArrivalDayWeighting = AGKey.[Key] ON Capacity.StartDate <= Time.Date AND Capacity.EndDate >= Time.Date
    CROSS JOIN
    (
        SELECT 0 AS col1
        UNION ALL
        SELECT 1 AS col1
    ) AS aaav
WHERE
    Time.CalendarYear = 2010
    AND (RAND(checksum(NEWID())) * Site.SiteWeighting * PitchType.PitchTypeWeighting >= 1.22)
于 2013-02-19T09:36:39.550 回答
0

我找到了解决方案,我不敢相信它是多么容易,我只是在查询设计器上取消了我不想要的框。

于 2013-02-19T12:51:20.953 回答