SQL Server查询异常行为

莫里亚

我在SQL Server中的查询遇到了奇怪的行为

我有两个表PrepaidTransactionsBillingData并且正在执行以下查询

Select * 
from PrepaidTransactions 
where customer_Id in
                  (Select customer_Id 
                   from BillingData 
                   where CommunityId = 10004)

该列customer_Id不属于table BillingData查询没有执行错误,而是执行并返回PrepaidTransactions表中的所有记录

但是当我运行以下查询时

Select customer_Id 
from BillingData 
where CommunityId = 10004

它显示一个错误

无效的列名“ customer_Id”。

谁能让我知道为什么第一个查询没有显示任何错误?

摩摩

我认为这两篇文章回答了您的问题。

https://connect.microsoft.com/SQLServer/feedback/details/542289/subquery-with-error-does-not-cause-outer-select-to-fail

http://support.microsoft.com/kb/298674

这是预期的行为,因为您的列名未绑定到表。因此,如果可以在外部表中进行解析(在您的查询中可以),则子查询不会失败。如果指定表BillingData.customer_Id,则会失败。文章说遵循这种做法以避免模棱两可。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章