sql-server

Filtering, Paging and Sorting in SQL Server 2008

This article provide one solution to achieve server side paging, sorting and filtering in SQL Server 2008.

Abhith Rajan
Abhith RajanNovember 22, 2018 · 2 min read · Last Updated:

Stored procedure to achieve paging, sorting and filtering in SQL Server 2008 is given below.

CREATE PROCEDURE [dbo].[uspTableNameOperationName] @Query      NVARCHAR(50) = NULL,
												   @Offset     INT          = 0,
												   @PageSize   INT          = 10,
												   @Sorting    NVARCHAR(20) = 'ID DESC',
												   @TotalCount INT OUTPUT
AS
	BEGIN
		SET NOCOUNT ON;
		SET @Query = LTRIM(RTRIM(@Query));
		SELECT @TotalCount = COUNT([Id])
		FROM [dbo].[TableName]
		WHERE IsDeleted = 0
			  AND (@Query IS NULL
				   OR [ColumnOne] LIKE '%' + @Query + '%'
				   OR [ColumnTwo] LIKE '%' + @Query + '%');
		WITH CTEResults
			 AS (SELECT Id,
						[ColumnOne],
						[ColumnTwo],
						[ColumnThree],
						ROW_NUMBER() OVER(
						ORDER BY CASE
									 WHEN(@Sorting = 'ID DESC')
									 THEN [Id]
								 END DESC,
								 CASE
									 WHEN(@Sorting = 'ID ASC')
									 THEN [Id]
								 END ASC,
								 CASE
									 WHEN(@Sorting = 'COLUMNONE ASC')
									 THEN [ColumnOne]
								 END ASC,
								 CASE
									 WHEN(@Sorting = 'COLUMNONE DESC')
									 THEN [ColumnOne]
								 END DESC,
								 CASE
									 WHEN(@Sorting = 'COLUMNTWO ASC')
									 THEN [ColumnTwo]
								 END ASC,
								 CASE
									 WHEN(@Sorting = 'COLUMNTWO DESC')
									 THEN [ColumnTwo]
								 END DESC) AS RowNum
				 FROM [dbo].[TableName]
				 WHERE IsDeleted = 0
						AND (@Query IS NULL
							OR [ColumnOne] LIKE '%' + @Query + '%'
							OR [ColumnTwo] LIKE '%' + @Query + '%');
		SELECT *
		FROM CTEResults
		WHERE RowNum BETWEEN @Offset + 1 AND(@Offset + @PageSize);
	END;
GO

In the above query, we are using CASE to do the conditional sorting. And with help of a common table expression (CTE), we do the paging. For filtering (here only string comparison), we are using LIKE.

If you are using SQL Server 2012 or later, then you have other options to do paging like by using OFFSET FETCH.

Story in short

During this post is written, I was working on an Angular 6 + ASP.NET Core 2.1 web app. The DB provided was a SQL Server 2008. Used Dapper to do the repository level coding.

Additional Resources

This page is open source. Noticed a typo? Or something unclear?
Improve this page on GitHub


Abhith Rajan

Written byAbhith Rajan
Abhith Rajan is a software engineer by day and a full-stack developer by night. He's coding for almost a decade now. He codes 🧑‍💻, write ✍️, learn 📖 and advocate 👍.
Connect

Is this page helpful?

Related SnippetsView All

Related ArticlesView All

Related VideosView All

High Availability and SLA for Azure SQL Managed Instance

Using SQL Data Sync for Bidirectional Data Replication in SQL Server & Azure SQL DBs | Data Exposed

Build fast, scalable data system on Azure SQL Database Hyperscale | Clearent

Related Tools & ServicesView All

sqlfum.pt

sequel fumpt

sqlfmt is an online SQL formatter. It is pronounced sequel fumpt. Its purpose is to beautifully format SQL statements.
SmarterASP.NET

SmarterASP.net - Unlimited ASP.NET Web Hosting

ASP.NET Hosting by SmarterASP.net. Unlimited ASP.NET Hosting Plans Starting at $2.95 a month.