The OFFSET and FETCH clause of SQL Server Denali provides you an option to fetch only a page or a window of the results from the complete result set.
Using this feature of SQL Server Denali one can easily implement SQL Server Paging while displaying results to the client. We will take a look at simple example and then also how you could construct a stored procedure to implement SQL paging.
Let’s go through a simple example which demonstrates how to use the OFFSET and FETCH feature of SQL Server Denali. You can see below that the TSQL looks the same as what you write today except after the ORDER BY clause we have the OFFSET and FETCH commands. One thing to note is that you have to use an ORDER BY to use this feature. The OFFSET basically tells SQL to skip the first 100 rows and the FETCH will get the next 5 rows.
The below snippet shows the output when running the above commands. This shows that the first 100 rows were discarded and the query fetched the next 5 rows in the complete recordset.
Let’s go through another example where we will create a stored procedure which will use the OFFSET and FETCHfeature of SQL Server Denali to achieve sql paging while displaying results to client machines. In this stored procedure we are passing in a page number and the number of rows to return. These values are then computed to get the correct page and number of rows.
Let’s go ahead and execute the stored procedure using the below command. This will give us five records starting at page 21 where the records are ordered by BusinessEntityID.
The below snippet shows the output once the above stored procedure is executed successfully. You can see that first 100 (20 pages * 5 rows per page = 100) rows were discarded and the stored procedure fetched only the next 5 rows thereby limiting the number of rows sent to the client.
You have seen in this tip how easily you can achieve SQL Server Paging using the OFFSET and FETCH feature of SQL Server Denali. SQL paging is not as hard as it used to be with this new feature.
Ref:http://www.mssqltips.com
Using this feature of SQL Server Denali one can easily implement SQL Server Paging while displaying results to the client. We will take a look at simple example and then also how you could construct a stored procedure to implement SQL paging.
Let’s go through a simple example which demonstrates how to use the OFFSET and FETCH feature of SQL Server Denali. You can see below that the TSQL looks the same as what you write today except after the ORDER BY clause we have the OFFSET and FETCH commands. One thing to note is that you have to use an ORDER BY to use this feature. The OFFSET basically tells SQL to skip the first 100 rows and the FETCH will get the next 5 rows.
The below snippet shows the output when running the above commands. This shows that the first 100 rows were discarded and the query fetched the next 5 rows in the complete recordset.
Let’s go through another example where we will create a stored procedure which will use the OFFSET and FETCHfeature of SQL Server Denali to achieve sql paging while displaying results to client machines. In this stored procedure we are passing in a page number and the number of rows to return. These values are then computed to get the correct page and number of rows.
Let’s go ahead and execute the stored procedure using the below command. This will give us five records starting at page 21 where the records are ordered by BusinessEntityID.
The below snippet shows the output once the above stored procedure is executed successfully. You can see that first 100 (20 pages * 5 rows per page = 100) rows were discarded and the stored procedure fetched only the next 5 rows thereby limiting the number of rows sent to the client.
You have seen in this tip how easily you can achieve SQL Server Paging using the OFFSET and FETCH feature of SQL Server Denali. SQL paging is not as hard as it used to be with this new feature.
Ref:http://www.mssqltips.com
No comments:
Post a Comment