我在 SQL Server 中的查询遇到了奇怪的行为
我有两个表 PrepaidTransactions
和 BillingData
,我正在执行以下查询
Select *
from PrepaidTransactions
where customer_Id in
(Select customer_Id
from BillingData
where CommunityId = 10004)
customer_Id
列不属于表 BillingData
。查询正在执行并返回 PrepaidTransactions
表
但是当我运行以下查询时
Select customer_Id
from BillingData
where CommunityId = 10004
显示错误
Invalid column name 'customer_Id'.
任何人都可以告诉我为什么第一个查询没有显示任何错误吗?
最佳答案
我认为这两篇文章回答了您的问题。
http://support.microsoft.com/kb/298674
这是预期行为,因为您的列名未绑定(bind)到表。因此,如果它可以在外表中解析(在您的查询的情况下,可以),那么子查询不会失败。如果指定表 BillingData.customer_Id,则会失败。文章说遵循这种做法以避免歧义。
关于SQL Server 查询奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22421049/