简体   繁体   中英

Problem with SQL subquery using Top() on Linked Server

I am using SQL Server 2008 and I have the following SQL script:

Select o.CustomerId as CustomerNoId, OrderValue, OrderDate
From dbo.Orders as o
Inner Join (
    Select Top (10) CustomerId
    From dbo.Customers
    where Age < 60
)
As c
On c.CustomerId = o.CustomerId

This works as desired when used with dbo.Customers and dbo.Orders on the local SQL Server instance. It returns all rows from the orders table for the first 10 customerIds returned from the the Customers table - 1688 rows.

However I have a linked server holding the Customers and Orders tables containing many more rows. When I modify the script to use dbo.Orders and dbo.Customers tables from the Linked Server I get a strange result - It appears the correct data is returned, but only the top 10 rows of it.

I am no SQL expert so I can't figure out why it should behave any differently.

Any suggestions appreciated.

Well there is a TOP (10) in your Subquery and no ORDER BY to boot, which means that you are not guaranteed to get the same 10 rows every time (this is especially true with linked servers because of the different algorithms that may be used for collation matching, even if the collations are the same).

Add an ORDER BY clause to the subquery so that you can make that part consistent and stable and the rest may follow correctly.

Firstly, your lack of an ORDER BY clause makes your sub-query non-deterministic, as @RBarryYoung pointed out.

Secondly, I would firstly try altering the join order (the sub-query becomes first table_source object for the FROM clause), and if not, try playing with the join hint REMOTE .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM