2013-02-19 118 views
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) 

感謝

韋恩

+3

錯誤很明顯:您的INSERT語句定義了** 3列**您的SELECT語句返回** 6列**這將**從不**工作!列**的數量必須匹配** – 2013-02-19 09:35:22

回答

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) 
+0

嗨凱文,那麼我如何只選擇我想插入到我的表中的結果? – wafw1971 2013-02-19 09:38:36

+1

@ wafw1971:只是**刪除**你的SELECT語句中那些你不需要的/不想要的列..... – 2013-02-19 09:43:29

+0

嗨凱文我對SQL很陌生,我在第四周的學習,所以我一直在問問題。 – wafw1971 2013-02-19 09:51:55

0

我已經找到了解決方案,我不敢相信是多麼容易,我只需在查詢設計器上取消選中我不想要的框。