I was trying to run an update for the table INVOICE.field1 = RECEIPT.field1, however there is no direct relationship between INVOICE and RECEIPT, but by the PURCHASE table that contains relations with both tables. I am using SQL Server 2022.
Below options don't work:
UPDATE INVOICE T1
SET T1.field1 = T3.field1
FROM PURCHASE T2
INNER JOIN RECEIPT T3 ON T2.field2 = T3.field2
WHERE T1.field3 = T2.field3
AND T1.field1 IN ('x')
or
UPDATE INVOICE T1
JOIN PURCHASE T2 ON T2.field2 = T3.field2
JOIN RECEIPT T3 ON T1.field3 = T2.field3
SET T1.field1 = T3.field1
WHERE T1.field1 IN ('x')
or
UPDATE INVOICE T1, PURCHASE T2, RECEIPT T3
SET T1.field1 = T3.field1
WHERE T1.field3 = T2.field3
AND T2.field2 = T3.field2
AND T1.field1 IN ('x')
I prefer to use the following syntax:
This way you can build your joins as needed and just refer to correct table in the beginning of the UPDATE.