sql - Strange behavior OFFSET ... ROWS FETCH NEXT ... ROWS ONLY after ORDER BY -
i see strange behavior of construction
select ... order ... offset ... rows fetch next ... rows
i made simple example display (sql server version: 2012 - 11.0.5058.0 (x64) ):
create table #temp( c1 int not null, c2 varchar(max) null) insert #temp (c1,c2) values (1,'test1') insert #temp (c1,c2) values (2,null) insert #temp (c1,c2) values (3,'test3') insert #temp (c1,c2) values (4,null) insert #temp (c1,c2) values (5,null)
first query:
select * #temp order c2 desc
result ok:
3 test3 1 test1 2 null 4 null 5 null
second query:
select * #temp order c2 desc offset 0 rows fetch next 3 rows
result has sorting:
3 test3 1 test1 4 null
and last query:
select * #temp order c2 desc offset 3 rows fetch next 3 rows
result strange , invalid in opinion (i need 2 records not contains in previous result, instead record id=4 second time):
4 null 2 null
can explain why sql server works strange?
use unique key column type of case:
select * #temp order c2, c1 desc offset 0 rows fetch next 3 rows
never faced ordering problem.
Comments
Post a Comment