我正在使用SQL Server 2014,并且正在运行以下UPDATE查询:
UPDATE ReservationStay SET ReasonForstayCode = b.ReasonForStayCode FROM MissingReasonForStay b WHERE ReservationStayID = b.ReservationStayID
该查询的目的是使用“ MissingReasonForStay”表中的数据更新“ ReservationStay”表中名为“ ReasonForStayCode”的列。查找需要使用两个表的“ ReservationStayID”列来完成。
问题是SSMS在代码的第4行下划线显示了ReservationStayID,并且当我运行查询时,我收到一条消息:列名“ ReservationStayID”不明确
我的代码有什么问题?
那是因为ReservationStayID
您的WHERE
子句中的条件不合格,并且SQL Server无法确定该列属于哪个表。
尝试这个
UPDATE a SET ReasonForstayCode = b.ReasonForStayCode FROM MissingReasonForStay b INNER JOIN ReservationStay a ON a.ReservationStayID = b.ReservationStayID