SQL将列设置为指定值,基于连接表中链接记录的值匹配

时间:2014-09-08 23:19:04

标签: sql sql-server sql-update

我在Microsoft SQL Server中尝试进行简单的SQL更新。我需要根据连接表中链接记录的值匹配将列设置为指定值。使用下面的SQL,我在关键字INNER'附近得到“不正确的语法”。我需要做什么,以及解释为什么我需要这样做,而不是我在这里尝试的方式:

UPDATE
    Core.Booking
INNER JOIN
    Core.BookingExtended be
    ON be.BookingId = BookingId
SET StandardFee = 11.50
WHERE
    ServiceId = 618
    AND be.BookingDate >= '1-sep-2014'
    AND FeeId = 7900

2 个答案:

答案 0 :(得分:2)

为SQL Server使用正确的语法:

UPDATE b
    SET StandardFee = 11.50
    FROM Core.Booking b INNER JOIN
         Core.BookingExtended be
         ON be.BookingId = b.BookingId
    WHERE ServiceId = 618 AND be.BookingDate >= '2014-09-01' AND FeeId = 7900;

答案 1 :(得分:1)

当两个表中都存在别名时,并非所有字段都与别名一起使用。尝试改为:

UPDATE bo
SET bo.StandardFee = 11.50
FROM
Core.Booking bo -- NEW ALIAS
INNER JOIN
    Core.BookingExtended be
    ON be.BookingId = bo.BookingId

WHERE
    bo.ServiceId = 618
    AND be.BookingDate >= '1-sep-2014'
    AND bo.FeeId = 7900