{"id":546,"date":"2019-08-09T12:19:07","date_gmt":"2019-08-09T06:49:07","guid":{"rendered":"https:\/\/techieshouts.com\/?p=546"},"modified":"2022-08-09T19:06:31","modified_gmt":"2022-08-09T13:36:31","slug":"pagination-in-sql-server","status":"publish","type":"post","link":"https:\/\/techieshouts.com\/home\/pagination-in-sql-server\/","title":{"rendered":"Pagination in SQL Server"},"content":{"rendered":"\n<p>In this article, let\u2019s see various methods to implement pagination in SQL server. <\/p>\n\n\n\n<p>In SQL server database, a table can have N no of records. Populating all these records in a grid control slows down the web page. You need to divide the number of records and populate it in the web page. <a href=\"https:\/\/en.wikipedia.org\/wiki\/Pagination\" target=\"_blank\" rel=\"noopener noreferrer\">Pagination<\/a> is the process of splitting the number of records from a SELECT query result set into several pages. Based on the SQL server version, it can be implemented using ROW_NUMBER function or OFFSET FETCH clause.<\/p>\n\n\n\n<p>Let&#8217;s\ncreate a sample table with some dummy data to see the implementation of\npagination.<\/p>\n\n\n\n<h2><strong>Create table with dummy data<\/strong><\/h2>\n\n\n\n<p>Create\na table &#8216;Course&#8217; and insert dummy data using the below queries<\/p>\n\n\n\n<pre data-mode=\"sqlserver\" data-theme=\"dreamweaver\" data-fontsize=\"14\" data-lines=\"Infinity\" class=\"wp-block-simple-code-block-ace\">CREATE TABLE Course\n(\n\tid INT PRIMARY KEY IDENTITY,\n\tcourseName VARCHAR(50) NOT NULL,\n\tdepartment VARCHAR(50) NOT NULL\n);\n\nDECLARE @count INT = 1, @depCount INT = 1\nWHILE (@count &lt;= 200)\nBEGIN\n\tINSERT INTO Course VALUES ('Course ' + LTRIM(@count), 'Department ' + LTRIM(@depCount))\n\tSET @count += 1;\n\tSET @depCount += 1;\n\tIF(@depCount % 10 = 0)\n\t\tSET @depCount = 1\nEND;<\/pre>\n\n\n\n<p>Now, the course table contains 200 records.<\/p>\n\n\n\n<h2><strong>Pagination using ROW_NUMBER function<\/strong><\/h2>\n\n\n\n<p>ROW_NUMBER function is introduced in SQL Server 2005. Let&#8217;s see how pagination can be implemented using this function. This method can be used in SQL server version 2005 and above.<\/p>\n\n\n\n<pre data-mode=\"sqlserver\" data-theme=\"dreamweaver\" data-fontsize=\"14\" data-lines=\"Infinity\" class=\"wp-block-simple-code-block-ace\">DECLARE @PageNo INT = 1\nDECLARE @RowsPerPage INT = 10 --Records Per Page\n\nDECLARE @Start INT, @End INT --Start and end record number in a page\nSET @Start = (@PageNo -1) * @RowsPerPage + 1\nSET @End = @RowsPerPage * @PageNo\n\nSELECT * FROM \n(\n\tSELECT *, ROW_NUMBER() OVER(ORDER BY department, id) AS RowNum \n\tFROM Course\n) AS Programs\nWHERE RowNum BETWEEN @Start AND @End;<\/pre>\n\n\n\n<p>See the result below. It returns the first page with 10 records.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" width=\"387\" height=\"344\" src=\"https:\/\/techieshouts.com\/wp-content\/uploads\/2019\/08\/Pagination-Result.png\" alt=\"Pagination in SQL using ROW_NUMBER function\" class=\"wp-image-582\" srcset=\"https:\/\/techieshouts.com\/wp-content\/uploads\/2019\/08\/Pagination-Result.png 387w, https:\/\/techieshouts.com\/wp-content\/uploads\/2019\/08\/Pagination-Result-300x267.png 300w\" sizes=\"(max-width: 387px) 100vw, 387px\" \/><figcaption>Pagination result &#8211; Page 1 shows first 10 records<\/figcaption><\/figure><\/div>\n\n\n\n<p>In the above script, change the value of the variable\n&#8216;@PageNo&#8217; to view various pages and the value of the variable &#8216;@RecordsPerPage&#8217;\nto show the records.<\/p>\n\n\n\n<pre data-mode=\"sqlserver\" data-theme=\"dreamweaver\" data-fontsize=\"14\" data-lines=\"Infinity\" class=\"wp-block-simple-code-block-ace\">DECLARE @PageNo INT = 3\nDECLARE @RowsPerPage INT = 15 --Records Per Page<\/pre>\n\n\n\n<p>See the result below after changing the variable values<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" width=\"463\" height=\"481\" src=\"https:\/\/techieshouts.com\/wp-content\/uploads\/2019\/08\/Pagination-Result-1.png\" alt=\"Pagination in SQL using ROW_NUMBER function\" class=\"wp-image-583\" srcset=\"https:\/\/techieshouts.com\/wp-content\/uploads\/2019\/08\/Pagination-Result-1.png 463w, https:\/\/techieshouts.com\/wp-content\/uploads\/2019\/08\/Pagination-Result-1-289x300.png 289w\" sizes=\"(max-width: 463px) 100vw, 463px\" \/><figcaption>Pagination result &#8211; Page 3 shows 15 records<\/figcaption><\/figure><\/div>\n\n\n\n<h2><strong>Pagination using OFFSET FETCH clause<\/strong><\/h2>\n\n\n\n<p>Another method to implement pagination in SQL server version 2012 or above is by using OFFSET FETCH clause. The Syntax of the OFFSET FETCH clause is <\/p>\n\n\n\n<pre data-mode=\"sqlserver\" data-theme=\"dreamweaver\" data-fontsize=\"14\" data-lines=\"Infinity\" class=\"wp-block-simple-code-block-ace\">SELECT columnName(s)\nFROM tableName\nORDER BY columnName\nOFFSET Rows_To_Skip\nFETCH NEXT Rows_To_Fetch ROWS ONLY;\n<\/pre>\n\n\n\n<p>The below query populates the first page with first 10 records.<\/p>\n\n\n\n<pre data-mode=\"sqlserver\" data-theme=\"dreamweaver\" data-fontsize=\"14\" data-lines=\"Infinity\" class=\"wp-block-simple-code-block-ace\">DECLARE @PageNo INT = 1\nDECLARE @RowsPerPage INT = 10 --Records Per Page\nDECLARE @RowsToSkip INT  =  (@PageNo -1) * @RowsPerPage\n\nSELECT * FROM Course \nORDER BY department, id \nOFFSET  @RowsToSkip ROWS\nFETCH NEXT @RowsPerPage ROWS ONLY;<\/pre>\n\n\n\n<p>And the result is,<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" width=\"332\" height=\"365\" src=\"https:\/\/techieshouts.com\/wp-content\/uploads\/2019\/08\/Pagination-Result-2.png\" alt=\"Pagination in SQL using OFFSET FETCH clause\" class=\"wp-image-584\" srcset=\"https:\/\/techieshouts.com\/wp-content\/uploads\/2019\/08\/Pagination-Result-2.png 332w, https:\/\/techieshouts.com\/wp-content\/uploads\/2019\/08\/Pagination-Result-2-273x300.png 273w\" sizes=\"(max-width: 332px) 100vw, 332px\" \/><figcaption>Pagination result using OFFSET FETCH clause<\/figcaption><\/figure><\/div>\n\n\n\n<p>Among the above mentioned methods, it is recommended to use the OFFSET FETCH method to implement pagination in SQL server 2012 and above for better performance.<\/p>\n\n\n\n\n\n<p>Also, check &#8220;<a href=\"https:\/\/techieshouts.com\/ms-sql-server-query-to-return-a-comma-separated-string-from-a-table\/\">Generate comma-separated string from a table<\/a>&#8220;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, let\u2019s see various methods to implement pagination in SQL server. In SQL server database, a table can have N no of records. Populating all these records in a grid control slows down the web page. You need to divide the number of records and populate it in the web page. Pagination is\u2026 <span class=\"read-more\"><a href=\"https:\/\/techieshouts.com\/home\/pagination-in-sql-server\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[10,15],"tags":[50,39,40,51,41],"_links":{"self":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/546"}],"collection":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/comments?post=546"}],"version-history":[{"count":8,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/546\/revisions"}],"predecessor-version":[{"id":593,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/546\/revisions\/593"}],"wp:attachment":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/media?parent=546"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/categories?post=546"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/tags?post=546"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}