August 20, 2010 at 10:40 am
My experience says the following code should fail:
update Tbl1
set Thing1 = (SELECT Tbl2.Thing2)
FROM Tbl1
INNER JOIN Tbl2
ON Tbl1.ID = Tbl2.ID
It doesn't. I think the closing bracket is wrong.Can someone please let me know why? I am puzzled.
Thanks
J
August 20, 2010 at 11:17 am
Why should it fail?
When you have subqueries within another query, the tables and columns in the outer query are fully visible in all of the subqueries. In your example, tbl2 is part of the outer query. Hence all the columns in tbl2 are visible in subqueries. Hence the reference to the table and column in your subquery binds to the table and column from the outer query.
This is documented and correct behaviour.
Your query is fully equivalent to this:
update Tbl1
set Thing1 = Tbl2.Thing2
FROM Tbl1
INNER JOIN Tbl2
ON Tbl1.ID = Tbl2.ID
No brackets or subqueries necessary.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply