SQL Server 2016 +链接服务器联接=未返回行

时间:2018-10-22 14:43:34

标签: sql-server sql-server-2016

我正在查询SQL Server 2016:

SELECT 1
FROM LINKEDSERVER1.DATABASE1.DBO.TABLE1 A with (nolock)
LEFT JOIN LINKEDSERVER1.DATABASE1.DBO.TABLE2 B with (nolock) ON ...
LEFT JOIN LINKEDSERVER1.DATABASE1.DBO.TABLE3 C with (nolock) ON ...
LEFT JOIN LINKEDSERVER1.DATABASE1.DBO.TABLE4 D with (nolock) ON (D.FIELD1 = C.FIELD1 AND D.CHAR_FIELD2 = B.VARCHAR_FIELD2 AND (D.FIELD3 = B.FIELD3 OR D.FIELD3 = B.FIELD4))
WHERE B.FIELD5 IN ('4472')

结果:未返回行

1-如果在联接之外更改条件 AND D.CHAR_FIELD2 = B.VARCHAR_FIELD2

SELECT 1
FROM LINKEDSERVER1.DATABASE1.DBO.TABLE1 A with (nolock)
LEFT JOIN LINKEDSERVER1.DATABASE1.DBO.TABLE2 B with (nolock) ON ...
LEFT JOIN LINKEDSERVER1.DATABASE1.DBO.TABLE3 C with (nolock) ON ...
LEFT JOIN LINKEDSERVER1.DATABASE1.DBO.TABLE4 D with (nolock) ON (D.FIELD1 = C.FIELD1 AND (D.FIELD3 = B.FIELD3 OR D.FIELD3 = B.FIELD4))
WHERE B.FIELD5 IN ('4472')
AND D.CHAR_FIELD2 = B.VARCHAR_FIELD2

结果:返回行

2-如果我在 TABLE4 上删除链接的服务器:

SELECT 1
FROM LINKEDSERVER1.DATABASE1.DBO.TABLE1 A with (nolock)
LEFT JOIN LINKEDSERVER1.DATABASE1.DBO.TABLE2 B with (nolock) ON ...
LEFT JOIN LINKEDSERVER1.DATABASE1.DBO.TABLE3 C with (nolock) ON ...
LEFT JOIN TABLE4 D with (nolock) ON (D.FIELD1 = C.FIELD1 AND D.CHAR_FIELD2 = B.VARCHAR_FIELD2 AND (D.FIELD3 = B.FIELD3 OR D.FIELD3 = B.FIELD4))
WHERE B.FIELD5 IN ('4472')

结果:返回行

3-如果我在SQL Server 2005上运行相同的查询:

SELECT 1
FROM LINKEDSERVER1.DATABASE1.DBO.TABLE1 A with (nolock)
LEFT JOIN LINKEDSERVER1.DATABASE1.DBO.TABLE2 B with (nolock) ON ...
LEFT JOIN LINKEDSERVER1.DATABASE1.DBO.TABLE3 C with (nolock) ON ...
LEFT JOIN LINKEDSERVER1.DATABASE1.DBO.TABLE4 D with (nolock) ON (D.FIELD1 = C.FIELD1 AND D.CHAR_FIELD2 = B.VARCHAR_FIELD2 AND (D.FIELD3 = B.FIELD3 OR D.FIELD3 = B.FIELD4))
WHERE B.FIELD5 IN ('4472')

结果:返回行

我正在运行SQL Server 2016 13.0.1601.5。 我在SP1和SP2上找不到任何相关内容。

这是一个已知问题吗?我想念什么吗?

1 个答案:

答案 0 :(得分:0)

发现了问题。我正在检查字段的排序规则,但是数据库的默认排序规则有所不同。

我猜因为这两个字段是char和varchar,所以它正在强制转换其中之一,并使用默认排序规则,然后在执行远程查询时,它找不到记录。

如果我更改默认排序规则或强制字段排序规则,它将起作用。如果在链接的服务器属性上禁用“使用远程排序”,它也可以工作。