问题
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.
回答1:
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.
回答2:
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
.
来源:https://stackoverflow.com/questions/1405547/problem-with-sql-subquery-using-top-on-linked-server