2

以下查询在 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)
4

1 回答 1

0

最简单的解释可能是这些a表有差异。行。

否则,我无法重现该问题。引用没有显式模式(例如 )的对象的查询SELECT * FROM A可能会给您带来差异。如果结果在同一台服务器上

1) 该查询由 diff 执行。用户。

2)用户被映射到差异。模式(例如 dbo 和 dbo2)。

3) diff 中有两个(或更多)同名的表/对象。模式(例如:dbo.Adbo2.A)。

在 SSMS 中运行此脚本并SQLCMD mode选中(菜单:查询 > SQLCMD 模式):

SET NOCOUNT ON;
CREATE DATABASE Test;
GO
USE Test;
GO

CREATE TABLE dbo.A(ID INT);
INSERT INTO dbo.A(ID) VALUES (1);
GO

CREATE SCHEMA dbo2;
GO
CREATE TABLE dbo2.A(ID INT);
INSERT INTO dbo2.A(ID) VALUES (1),(2);
GO
-- Test database has two tables [A] with diff. schema (dbo, dbo2)

USE master;
GO
CREATE LOGIN login_test_1 WITH PASSWORD='pas$w0Rd', DEFAULT_DATABASE=Test;
GO
CREATE LOGIN login_test_2 WITH PASSWORD='pas$w0Rd', DEFAULT_DATABASE=Test;
GO
USE Test;
GO
-- This user is mapped to schema [dbo]
CREATE USER user_test_1 FOR LOGIN login_test_1 WITH DEFAULT_SCHEMA=dbo;  
GO
GRANT SELECT ON dbo.A TO user_test_1;
GO
GRANT SELECT ON dbo2.A TO user_test_1;
GO
-- This user is mapped to schema [dbo2] (!)
CREATE USER user_test_2 FOR LOGIN login_test_2 WITH DEFAULT_SCHEMA=dbo2;
GO
GRANT SELECT ON dbo.A TO user_test_2;
GO
GRANT SELECT ON dbo2.A TO user_test_2;
GO

-- Test login_test_1
:CONNECT (local)\SQL2008R2 -U login_test_1 -P pas$w0Rd
SELECT USER_NAME() AS CurrentUser;
SELECT SCHEMA_NAME() AS CurrentSchema;
SELECT  *
FROM    A;
GO

-- Test login_test_2
:CONNECT (local)\SQL2008R2 -U login_test_2 -P pas$w0Rd
SELECT USER_NAME() AS CurrentUser;
SELECT SCHEMA_NAME() AS CurrentSchema;
SELECT  *
FROM    A;
GO
SET NOCOUNT OFF;

结果:

Connecting to (local)\SQL2008R2 as login_test_1...
CurrentUser
--------------------------------------------------------------------------------------------------------------------------------
user_test_1
CurrentSchema
--------------------------------------------------------------------------------------------------------------------------------
dbo
ID
-----------
1
Disconnecting connection from (local)\SQL2008R2 as login_test_1...

Connecting to (local)\SQL2008R2 as login_test_2...
CurrentUser
--------------------------------------------------------------------------------------------------------------------------------
user_test_2
CurrentSchema
--------------------------------------------------------------------------------------------------------------------------------
dbo2
ID
-----------
1
2
Disconnecting connection from (local)\SQL2008R2 as login_test_2...

在这种情况下,login_test_1 (dbo) 获得1行,而 login_test_2 (dbo2) 获得2行。

清理脚本:

/*
DROP DATABASE Test;
DROP LOGIN login_test_1;
DROP LOGIN login_test_2;
GO
*/
于 2013-07-22T13:11:45.320 回答