Data deleted with IN clause even if the column doesn't exist

心已入冬 提交于 2019-12-02 04:14:13

It's rather well-known behaviour. I'm not saying it's not somewhat unexpected.

The thing is that SQL Server tries to bind names to objects as one of first things id does with the query. With a subquery, it tries to bind columns to tables in the subquery first; if it don't succeed, it will bind columns to tables in outer query.

It's actually an excellent reason to always use aliases with tables.

To check which kind of rows are deleted from A, you can query

SELECT *
FROM A
WHERE xxx_id IN (SELECT xxx_id FROM B)

To check what values are in sub query (SELECT xxx_id FROM B), you can query

SELECT xxx_id
FROM A, B

Which equals to

SELECT A.xxx_id,
FROM A, B

Now you should know the reason.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!