2013-07-19 58 views
2

下面的查詢返回的SQL Server 2008 R2的兩個實例兩種不同的結果:不同的行爲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年7月22日:

DBCC USEROPTIONS; SELECT @@ 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年7月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) 
+0

運行DBCC'USEROPTIONS;在這兩種情況下'並公佈結果; SELECT @@ VERSION。 –

+1

添加結果... – user2598804

+0

This([link](http://support.microsoft.com/kb/298674/en-us))是這種情況下預期的行爲(子查詢)。嘗試在兩個數據庫上運行此查詢'SELECT * FROM sys.databases d WHERE d.database_id = DB_ID()'並比較結果。請告訴我,如果你發現了什麼。 –

回答

0

最簡單的解釋可能是那些a表有差異。行。

否則,我不能重現該問題。引用沒有顯式模式的對象的查詢(例如SELECT * FROM A)可以爲您提供差異。結果相同服務器如果

1)該查詢由diff執行。用戶。

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

3)在diff中有兩個(或多個)具有相同名稱的表/對象。模式(例如:dbo.Adbo2.A)。

運行該腳本與SQLCMD mode SSMS選擇(菜單:查詢> 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)得到行但login_test_2(dbo2)獲得行。

清理腳本:

/* 
DROP DATABASE Test; 
DROP LOGIN login_test_1; 
DROP LOGIN login_test_2; 
GO 
*/ 
+1

添加了一些細節 – user2598804