以下查询在 SQL Server 2008 R2 的两个实例上返回两个不同的结果:
create table a(id int)
insert into a(id)
values(1)
insert into a(id)
values(2)
select
id,
(select count(dbo.a.id) from dbo.a where dbo.a.id = "a"."id")
from a
where a.id = 1
第一台机器给
id
----------- -----------
1 2
第二台机器给
id
----------- -----------
1 1
我们知道如何通过在子查询中使用显式别名来解决这个问题。但是因为我们经常使用这样的结构,所以这将是一项巨大的工作。所以我们想了解这个问题。
SQL Server 中是否有一个选项可以控制这种行为?
2013/07/22:
DBCC 用户选项;选择@@VERSION;给
Set Option Value
----------------------------- ----------------
textsize 2147483647
language Deutsch
dateformat dmy
datefirst 1
lock_timeout -1
quoted_identifier SET
arithabort SET
ansi_null_dflt_on SET
ansi_warnings SET
ansi_padding SET
ansi_nulls SET
concat_null_yields_null SET
isolation level read committed
------------------------------------
Microsoft SQL Server 2008 R2 (SP1) - 10.50.2500.0 (Intel X86)
Jun 17 2011 00:57:23
Copyright (c) Microsoft Corporation
Enterprise Edition on Windows NT 6.0 <X86> (Build 6002: Service Pack 2)
和
Set Option Value
----------------------------- ----------------
textsize 2147483647
language Deutsch
dateformat dmy
datefirst 1
lock_timeout -1
quoted_identifier SET
arithabort SET
ansi_null_dflt_on SET
ansi_warnings SET
ansi_padding SET
ansi_nulls SET
concat_null_yields_null SET
isolation level read committed
------------------------------------
Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (X64)
Apr 2 2010 15:48:46
Copyright (c) Microsoft Corporation
Standard Edition (64-bit) on Windows NT 5.2 <X64> (Build 3790: Service Pack 2)
对于第一台服务器,查询按照我们想要的方式工作。
2013/07/24
它似乎不依赖于服务器,而是依赖于数据库。
服务器:
Set Option Value
---------------------------- ----------------------------------------------
textsize 2147483647
language Deutsch
dateformat dmy
datefirst 1
lock_timeout -1
quoted_identifier SET
arithabort SET
ansi_null_dflt_on SET
ansi_warnings SET
ansi_padding SET
ansi_nulls SET
concat_null_yields_null SET
isolation level read committed
(13 Zeile(n) betroffen)
Die DBCC-Ausführung wurde abgeschlossen. Falls DBCC Fehlermeldungen ausgegeben hat, wenden Sie sich an den Systemadministrator.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Microsoft SQL Server 2008 R2 (SP1) - 10.50.2500.0 (Intel X86)
Jun 17 2011 00:57:23
Copyright (c) Microsoft Corporation
Enterprise Edition on Windows NT 6.0 <X86> (Build 6002: Service Pack 2)
(1 Zeile(n) betroffen)
以下查询:
create table a(id int)
insert into a(id)
values(1)
insert into a(id)
values(2)
select * from a
select
id,
(select count(dbo.a.id) from dbo.a where dbo.a.id = "a"."id")
from a
where a.id = 1
drop table a
SELECT USER_NAME() AS CurrentUser;
SELECT SCHEMA_NAME() AS CurrentSchema;
第一个数据库给出:
id
-----------
1
2
(2 Zeile(n) betroffen)
id
----------- -----------
1 2
(1 Zeile(n) betroffen)
CurrentUser
--------------------------
dbo
(1 Zeile(n) betroffen)
CurrentSchema
--------------------------
dbo
(1 Zeile(n) betroffen)
第二个数据库给出:
id
-----------
1
2
(2 Zeile(n) betroffen)
id
----------- -----------
1 1
(1 Zeile(n) betroffen)
CurrentUser
-----------------------
dbo
(1 Zeile(n) betroffen)
CurrentSchema
-----------------------
dbo
(1 Zeile(n) betroffen)