Sorry for the bad title - I can't think of a better one.
Essentially, SQL Server 2008 is processing a sub-query that I would have expected to fail.
Here's a simple example:
CREATE TABLE UserDemographics
(
UserID INT,
UserName NVARCHAR(500)
)
CREATE TABLE UserAddresses
(
UserAddressID INT,
Address NVARCHAR(500)
)
INSERT INTO UserDemographics(UserID, UserName) VALUES(1, 'Joe'), (2, 'Sam'), (3, 'Ed')
INSERT INTO UserAddresses(UserAddressID, Address) VALUES(1, '1st Street'), (2, 'Old Address 1'), (3, 'Old Address 2')
DELETE FROM UserDemographics
WHERE UserID IN
(
SELECT UserID FROM UserAddresses
)
Here is why this is interesting: UserAddresses
does not have a column called UserID
. The correlated sub-query is selecting a column that does not exist in the table it is querying.
Obviously, it has something to do with the correlated-sub-query - UserID
is a column in the UserDemographics
table, and for some reason the database is making that reference.
Perhaps the UserDemographics
table is implied in the from-list in the sub-query.
Can anyone explain this?