如何在SQL Server上用多个Joins更新表?在MySQL中,您可以为更新的表定义别名,但是在TSQL中如何实现呢?
UPDATE recert.ou --#1-- In MSSQL/TSQL no alias allowed
SET parent_id = o2.ID
FROM recert.ou as O
JOIN recert.country C ON C.ID = O.country_id
JOIN recert.ou P ON O.parent_id = P.ID and p.country_id <> O.country_id
JOIN recert.ou o2 on o2.name = p.name and c.ID = o2.country_id
JOIN recert.country as c2 on c2.ID = o2.country_id
WHERE O.ID = o2.ID
-
RESULT: *The table 'o' is ambiguous.*
o
别名为o
? - Nikola MarkovinovićUPDATE O1 SET parent_id = O2.ID FROM o AS O1 blah blah blah...
会发生什么?TSQL允许在UPDATE
语句中出现别名。但我以前从未尝试过将表的别名设置为其自身的名称。 - MatBailieFROM recert.o AS o
- hoerfMsg 8154, Level 16, State 1, Line 2 The table 'recert.o' is ambiguous.
这是因为JOIN recert.o P
和JOIN recert.o o2
引起的。 - hoerf