How to make paging in Sqlserver 2008 R2?

Asked

Viewed 1,365 times

3

I tested with two forms, but did not succeed.

select * from table OFFSET 10 ROWS

select top 10 * from table OFFSET 10 ROWS FETCH NEXT 5 ROWS ONLY

Interestingly I never had to offset this version of Sqlserver, and now I’m having difficulties with something relatively simple.

Errors displayed on console:

Message 102, Level 15, Status 1, Line 3
Incorrect syntax near 'OFFSET'.
Message 153, Level 15, State 2, Line 3
Invalid Usage of the option NEXT in the FETCH statement.

I might be wrong, but I confess that I did not find answers on the site that really answer my question (which is curious, since it is something relatively simple).

  • 1

    I want to be wrong, but I believe that LIMIT does not work on Sqlserver. This is a MYSQL command, isn’t it? I at least use TOP 10 in Sqlserver, and LIMIT 10 in MYSQL.

  • The constructions you used only work in the 2012 (onward) version of SQL Server.

  • See "Comparing performance for Different SQL Server paging methods" https://www.mssqltips.com/sqlservertip/2696/comparing-performance-for-different-sql-server-paging-methods/

2 answers

1

There are two ways to do this. Let’s assume that the query you will make is this:

SELECT * FROM Orders WHERE OrderDate >= '1980-01-01' ORDER BY OrderDate

In this case, Voce should determine the total results using:

SELECT COUNT(*) FROM Orders WHERE OrderDate >= '1980-01-01'

...may be inappropriate considering the efficiency of the result.

Now, in order to achieve a more appropriate consultation, the following model could be the most efficient:

SELECT  *
FROM    ( SELECT    ROW_NUMBER() OVER ( ORDER BY OrderDate ) AS RowNum, *
          FROM      Orders
          WHERE     OrderDate >= '1980-01-01'
        ) AS RowConstrainedResult
WHERE   RowNum >= 1
    AND RowNum < 20
ORDER BY RowNum

Here it will return 1-19 lines of the original query. Now, just adapt to your code.

0

Thiago, To paginate it is necessary an ordination (ORDER BY) of the records for your case:

SELECT * FROM table 
ORDER BY 1 --Nome campo
OFFSET 10 ROWS FETCH NEXT 5 ROWS ONLY
  • I even tested using order by as well, but I get the following errors: Message 102, Level 15, Status 1, Line 3 Incorrect syntax near 'OFFSET'. Message 153, Level 15, State 2, Line 3 Invalid Usage of the option NEXT in the FETCH statement.

  • Thiago, you haven’t forgotten ORDER BY? because Offset is an additional parameter of ORDER BY. Follow microsoft doc with function documentation [link] (https://docs.microsoft.com/en-us/sql/t-sql/queries/select-order-by-clause-transact-sql)

  • I tried yes! The query I tried to run was this: SELECT TOP 10 * FROM users ORDER BY id OFFSET 10 ROWS FETCH NEXT 5 ROWS ONLY

  • It’s still not working! SELECT * FROM Personal...

  • The bank is at compatibility level?

  • To be honest I had never accessed the property, and for once I am not the SA of the bank... But this implies that?

  • 1

    So in the microsoft documentation it was written from SQL 2008 but when reading the document more in depth, the Offset function is available from the 2012 version of SQL server. https://docs.microsoft.com/en-us/sql/t-sql/queries/select-order-by-clause-transact-sql#Offset

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.