668
Views
7
Comments
Making pagination for SQL query table
Question
Application Type
Reactive

Hello guys,..... So i have a table that uses an SQL query meaning I cannot use the pagination widget on it..... I am struggling writing SQL code to make the pagination functional.. Please help

2022-02-25 19-42-59
Srinivas K Singadi

Hi Ide Fatsha

 you can implement pagination in your SQL query using the OFFSET and FETCH NEXT clauses.

Here is an example of how you can modify your SQL query to implement pagination:

sql

Copy code

SELECT *

FROM your_table

ORDER BY your_column

OFFSET (@pageNumber - 1) * @pageSize ROWS

FETCH NEXT @pageSize ROWS ONLY

In the above syntax, your_table is the name of the table you want to query, your_column is the column you want to use to order the results, @pageNumber is the number of the page you want to retrieve (starting from 1), and @pageSize is the number of rows you want to retrieve per page.

The OFFSET clause skips the first (@pageNumber - 1) * @pageSize rows, and the FETCH NEXT clause retrieves the next @pageSize rows. By combining these clauses with the ORDER BY clause, you can retrieve the rows in the desired order and retrieve only the rows for the requested page

2025-10-18 11-13-53
Ramesh subramanian

Hi Ide Fatsha,

Please find the demo OML File.

Thanks,

Ramesh



TableAndAdvancedSql.oml
UserImage.jpg
shooouse

Hi Ramesh,

If I test your SQL Query, I'm getting the following error message: 
"Database returned the following error: Error in advanced query SQL1: Incorrect syntax near '@qpinStartIndex'. Invalid usage of the option NEXT in the FETCH statement. "

However, If I publish it, your solution works. 

I don't understand, how is that possible? 

Thanks,

Laura

2023-06-14 21-26-42
oscarldrt


I had the same problem and managed to solve it by passing a default value to the TableSort variable.

2024-02-04 14-12-04
Margarida Magos

Hello Ide,

The issue is not in the NEXT in the FETCH statement, the issue is in the ORDER BY clause.

You need to always have a default Sort By, or else the clause of ORDER BY your_column will give an error because the your_column is blank. 

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.