查询帮助和错误

时间:2015-03-18 22:48:12

标签: sql sql-server

我试图实施查询以找到西班牙最便宜的套餐。

我已加入酒店,游览,交通和包裹上的航班表,并从每个表中总计了总成本。

但是在输入我的值时会收到错误消息,并且必须为表格中的标识列指定显式值' package'当IDENTITY_INSERT设置为ON或复制用户插入NOT FOR REPLICATION标识列时。例如

  SET IDENTITY_INSERT package ON
           Insert Into package (hotelID, excursionID, transportID, flightID)
           Values (1, 1, 1, 1)
           Insert Into package (hotelID, excursionID, transportID, flightID)
           Values (2, 2, 2, 2)
           Insert Into package (hotelID, excursionID, transportID, flightID)
           Values (3, 3, 3, 3)
           Insert Into package (hotelID, excursionID, transportID, flightID)
           Values (4, 4, 4, 4)
           Insert Into package (hotelID, excursionID, transportID, flightID)
           Values (5, 5, 5, 5)
           Insert Into package (hotelID, excursionID, transportID, flightID)
           Values (6, 6, 6, 6)
           Insert Into package (hotelID, excursionID, transportID, flightID)
           Values (7, 7, 7, 7)
           Insert Into package (hotelID, excursionID, transportID, flightID)
           Values (8, 8, 8, 8)
           Insert Into package (hotelID, excursionID, transportID, flightID)
           Values (9, 9, 9, 9)
           Insert Into package (hotelID, excursionID, transportID, flightID)
           Values (10, 10, 10, 10)

           SET IDENTITY_INSERT package OFF

这是我的查询

      SELECT p.*, (h.totalCost + e.totalCost + t.totalCost, f.totalCost) as    'Total Package Cost' FROM Package p
             INNER JOIN hotel h ON h.hotelId = p.hotelId
             INNER JOIN excursion e ON e.excursionId = p.excursionId
             INNER JOIN transport t ON t.transportId = p.transportId
             INNER JOIN flight f ON f.flightId = p.flightId
      WHERE h.location = 'Spain'
      ORDER BY (h.totalCost + e.totalCost + t.totalCost, f.totalCost) ASC

页。带下划线(不匹配表名)和h。 (在' h'附近的语法不正确。底部的期望'('或SELECT。)带有下划线。

有人可以帮我解决这个问题,谢谢!

1 个答案:

答案 0 :(得分:2)

第一部分:

插入"包" table:看起来" package"中有标识列。               并且在此插入查询的第一行,您要设置" SET IDENTITY_INSERT包ON"               这意味着您尝试在标识列中插入值但不提供标识列名称。

尝试删除" SET IDENTITY_INSERT包"从第一行开始        和" SET IDENTITY_INSERT包OFF"从最后一行

   then execute rest of the insert query. 

如果仍然收到错误。 您能否分享一下"创建包表的表格模式"?

第二部分:

同时添加" totalCost"从不同的表中删除","之间    您的查询应如下所示:

   SELECT p.*, (h.totalCost + e.totalCost + t.totalCost + f.totalCost) as    'Total Package Cost' FROM Package p
             INNER JOIN hotel h ON h.hotelId = p.hotelId
             INNER JOIN excursion e ON e.excursionId = p.excursionId
             INNER JOIN transport t ON t.transportId = p.transportId
             INNER JOIN flight f ON f.flightId = p.flightId
      WHERE h.location = 'Spain'
      ORDER BY (h.totalCost + e.totalCost + t.totalCost + f.totalCost) ASC