I sometimes need to run basic updates on a join. For example:
UPDATE t1 SET col1 = 'val1'
FROM table1 as t1
INNER JOIN table2 as t2
ON t1.ID = t2.t1_id
WHERE t2.col3 = 'val3'
This works perfectly, but for some reason, in MS SQL Management Studio Express, it wants to convert this to
UPDATE t1 SET col1 = 'val1'
FROM table1 as t1
INNER JOIN table2 as t2
ON t1.ID = t2.t1_id
CROSS JOIN t2
WHERE t2.col3 = 'val3'
It adds a crossjoin for some reason that I don't understand.
Now my question is: Why does Manageme开发者_StackOverflow中文版nt Studio think this is what I meant? It must have a genuine use, otherwise it wouldn't suggest it. Yet I have no idea how and when (and why).
It must be a bug, because your fist code is rigth.
Try
Update t1 set col1 = 'val1' from table1 t1, table2 t2 where t1.id = t2.t1_id and t2.col3='val3'
It throws the same result, but it could be less efficient. Does your SQL-MS add any code? Strange...
According to MSDN a cross join like that is equivalent to an inner join. Perhaps it uses cross joins because a cross join can be used to create cartesian products as well as simpler joins - whereas an inner join is more limited.
精彩评论