插入语句错误消息

时间:2013-02-19 09:32:51

标签: sql-server tsql insert-update

早安全部

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

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

  

Msg 121,Level 15,State 1,Line 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)

由于

韦恩

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

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