SQL Server 查询奇怪的行为

标签 sql sql-server execution query-expressions

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

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

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'.

任何人都可以告诉我为什么第一个查询没有显示任何错误吗?

最佳答案

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

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

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

这是预期行为,因为您的列名未绑定(bind)到表。因此,如果它可以在外表中解析(在您的查询的情况下,可以),那么子查询不会失败。如果指定表 BillingData.customer_Id,则会失败。文章说遵循这种做法以避免歧义。

关于SQL Server 查询奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22421049/

相关文章:

c - 当我第二次运行该程序时,它没有给我正确的执行时间

php - 如何执行大型 PHP 脚本?

mysql - 在 MySQL 中,如何将 JSON_OBJECT 中的 VARCHAR 转换为 UNSIGNED?

mysql - 在一行 SQL 中更新两行不同的行

php - 如何在 Linux 上使用 PHP 连接到远程 MSSQL 数据库?

sql - ORDER BY 之前没有空格 - 为什么这有效?

php - 如果不需要进行查询,冗余数据是否可以?

sql - 生成优惠券代码时感到困惑

sql-server - 从 SQL Server 中的 xml 字段中选择日期列表

python - 运行能够与主程序通信但与系统隔离的不受信任的 python 代码