SQL SERVER - FETCH NEXT ROWS返回重复的结果

时间:2017-07-05 19:21:39

标签: sql-server

我的网站上有一个非常奇怪的问题,最后我成功地确定了确切的原因。

在我的实体' table我有几个列,例如" Id"," Title"," Date"," Indexing"还有一些。

"索引"用全文索引索引的列。

考虑以下SQL查询:

SELECT Id 
FROM [dbo].[Entities] AS DocTable 
INNER JOIN CONTAINSTABLE(Entities, Indexing, 'otherCriteria OR criteia') AS KEY_TBL  
ON DocTable.Id = KEY_TBL.[KEY] 
ORDER BY KEY_TBL.RANK DESC

此查询按预期工作,我得到大约600个结果。

现在我添加分页:

SELECT Id 
FROM [dbo].[Entities] AS DocTable 
INNER JOIN CONTAINSTABLE(Entities, Indexing, 'otherCriteria OR criteia') AS KEY_TBL  
ON DocTable.Id = KEY_TBL.[KEY] 
ORDER BY KEY_TBL.RANK DESC 
OFFSET 50 ROWS FETCH NEXT 10 ROWS ONLY

无论我使用OFFSET 30还是40或50 - >我总是得到完全相同的结果!

现在真是奇怪的部分:如果在SELECT内部,我要求选择' Title'也(例如,SELECT Id,Title) - >我得到了正确的结果。

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

好的,我想我明白了

In Sql Server 2014 ORDER BY clause with OFFSET FETCH NEXT returns weird results

说:

  

ORDER BY子句包含一列或一组列   保证是独一无二的。

显然,"排名"不是唯一的。当我将查询更改为:

SELECT Id 
FROM [dbo].[Entities] AS DocTable 
INNER JOIN CONTAINSTABLE(Entities, Indexing, 'otherCriteria OR criteia') AS KEY_TBL  
ON DocTable.Id = KEY_TBL.[KEY] 
ORDER BY KEY_TBL.RANK DESC, Id
OFFSET 50 ROWS FETCH NEXT 10 ROWS ONLY

我收到了正确的结果