我有两张桌子,其中一张我不再需要了。我想将我需要的数据从过时的表中传输到我要保留的表中。两个表中都有bookingid
列,我可以使用这些列来匹配行。它是1到0或1的关系。我环顾四周并建立了这个查询以完成转移,但我在bookingtoupdate.bookingid
WITH bookingtoupdate (bookingid) AS
(
SELECT bookingid
FROM bookings
)
UPDATE bookings
SET meetinglocation = (SELECT business.name
FROM abk_Locations
INNER JOIN business ON dbo.abk_Locations.IP_Number = business.businessid
WHERE
(dbo.abk_Locations.Booking_Number = bookingtoupdate.bookingid)
)
WHERE
bookingid = bookingtoupdate.bookingid
我的代码有任何明显的问题吗?
我参考了以下几页......
http://msdn.microsoft.com/en-us/library/ms175972.aspx SQL Server FOR EACH Loop
答案 0 :(得分:2)
您声明bookingtoupdate
但不从中选择任何内容。这就是它无法受约束的原因。
这是一个简化的查询,可以在没有CTE的情况下完成所需的操作
UPDATE bookings
SET meetinglocation = business.name
FROM bookings
INNER JOIN abk_Locations ON abk_Locations.Booking_Number = bookings.bookingid
INNER JOIN business ON dbo.abk_Locations.IP_Number = business.businessid