Tag Archives: CTE

Data Paging by OFFSET…FECTCH and CTE – Sql Server

When we have huge result set it doesn’t make sense to return the complete result set to the application and leave it to the datagrid, gridview or any other data control to handle the data paging. Because it wastes lot of network resources, puts pressure on the database server and the application server, so it makes page loading very slow. In such scenario it makes sense to return only the rows which needs to displayed on the current page, when user clicks next page/next result set then bring the next set of rows. In this way it reduces the unnecessary network traffic and load on the data base server, which in-turn helps in faster loading of the page.

With Sql Server 2012 Microsoft has introduced OFFSET and FETCH feature for data paging. Prior to Sql Server 2012 CTE was on of the option for data paging.

To demonstrate data paging let us create a customers table as shown in the below image by the following script:

Data Paging Using CTE
Script:

--Create Demo database
CREATE DATABASE SqlHintsCTEDataPaging
GO
USE SqlHintsCTEDataPaging
GO
-- Create an Customers table.
CREATE TABLE dbo.Customers
( Id INT  PRIMARY KEY, FirstName NVARCHAR(50),
  LastName NVARCHAR(50))
-- Populate Customer table with sample data
INSERT INTO dbo.Customers VALUES
 (1, 'Basavaraj', 'Biradar') ,(2, 'Abhishiek', 'Akkanna')
,(3, 'Santosh', 'Patil'), (4, 'Sachin', 'Tendulkar')
,(5, 'Virendra', 'Shewag'),(6, 'Virat', 'Kohli')
,(7, 'Ajinkya', 'Rahane'), (8,'Cheteshwar', 'Pujara')

Data Paging by OFFSET/FETCH

We can write a stored procedure like below for Data Paging by using the OFFSET..FETCH pagination feature introduced in Sql Server 2012. This stored procedure gives specified number of rows (i.e. it can be specified by the parameter @pageSize) for the passed page number (i.e. parameter @pageNum) sorted by the FirstName column.

CREATE PROCEDURE dbo.GetCustomersPagedDatabyFetch
(
  @pageNum INT,
  @pageSize INT
)
AS
BEGIN
	SELECT Id, FirstName, LastName
	FROM dbo.Customers WITH(NOLOCK)
	ORDER BY FirstName
	OFFSET (@pageNum - 1) * @pageSize ROWS
	FETCH NEXT @pageSize ROWS ONLY  
END

Try to get the first page data sorted by FirstName by executing the above Stored Procedure, assume that the page size as 3 for our example.

EXEC dbo.GetCustomersPagedDatabyFetch 
         @pageNum = 1, @pageSize = 3

RESULT:
Data Paging by OFFSET and FETCH Page 1

Now try to get the second page data sorted by FirstName by executing the below statement.

EXEC dbo.GetCustomersPagedDatabyFetch 
         @pageNum = 2, @pageSize = 3

RESULT:
Data Paging by OFFSET and FETCH Page 2

Now try to get the third page data sorted by FirstName by executing the below statement.

EXEC dbo.GetCustomersPagedDatabyFetch 
         @pageNum = 3, @pageSize = 3

RESULT:
Data Paging by OFFSET and FETCH Page 3

The reason we got only two records in the above result even when the data page size 3 is, the demo Customers table created in this article has only 8 records. So, with page size as 3 the third page will have only two records.

Data paging by CTE

We can write a stored procedure like below for Data Paging by CTE. This stored procedure gives specified number of rows (i.e. it can be specified by the parameter @pageSize) for the passed page number (i.e. parameter @pageNum) sorted by the FirstName column.

CREATE PROCEDURE dbo.GetCustomersPagedData
(
	@pageNum INT,  -- Data page number
	@pageSize INT  -- Number of rows per page 
)
AS
BEGIN
	WITH PagingCTE AS
	(
	 SELECT Id, FirstName, LastName, 
          ROW_NUMBER() OVER (ORDER BY FirstName) AS RowNumber
	 FROM dbo.Customers WITH(NOLOCK)
	)
	SELECT *
	FROM PagingCTE
	WHERE RowNumber BETWEEN (@pageNum - 1) * @pageSize + 1 
              AND @pageNum * @pageSize	
END

Try to get the first page data sorted by FirstName by executing the above Stored Procedure, assume that the page size as 3 for our example.

EXEC dbo.GetCustomersPagedData @pageNum = 1, @pageSize = 3

RESULT:
Data Paging by CTE

Now try to get the second page data sorted by FirstName by executing the below statement.

EXEC dbo.GetCustomersPagedData @pageNum = 2, @pageSize = 3

RESULT:
Data Paging by CTE data page 2

Now try to get the third page data sorted by FirstName by executing the below statement.

EXEC dbo.GetCustomersPagedData @pageNum = 3, @pageSize = 3

RESULT:
Data Paging by CTE data page 3

The reason we got only two records in the above result even when the data page size 3 is, the demo Customers table created in this article has only 8 records. So, with page size as 3 the third page will have only two records.

Data paging by CTE with dynamic Sort column

In the previous example of this article the SP GetCustomersPagedData was supporting the data paging but the sort column here was always the FirstName column. But in real world we will always not have the data paging with sorting by just one column. For example, for the customers table we may need to support data paging with either FirstName or LastName as the sorting column. We can create a SP like below which supports the data paging by the FirstName or LastName as the sort column by using CTE:

CREATE PROCEDURE dbo.GetCustomersDynamicSortColumn
(
	@pageNum INT,
	@pageSize INT,
	@sortColumnName VARCHAR(50)
)
AS
BEGIN
  WITH PagingCTE AS
  (
    SELECT Id, FirstName, LastName, ROW_NUMBER() OVER 
     (ORDER BY CASE 
       WHEN @sortColumnName = 'FirstName' THEN  FirstName 
       WHEN @sortColumnName = 'LastName' THEN  LastName 
						 END) AS RowNumber
    FROM dbo.Customers WITH(NOLOCK)
  )
  SELECT *
  FROM PagingCTE
  WHERE RowNumber BETWEEN (@pageNum - 1) * @pageSize + 1 
   AND @pageNum * @pageSize
END

Now try to execute the above stored procedure to get the first page data with page size as 3 and sort column as FirstName:

EXEC dbo.GetCustomersDynamicSortColumn 
      @pageNum = 1, @pageSize = 3, @sortColumnName = 'FirstName'

RESULT:
Data Paging by CTE dynamic Sort Column

Now execute the following statement to get the first page data with page size as 3 and sort column as LastName:

EXEC dbo.GetCustomersDynamicSortColumn 
      @pageNum = 1, @pageSize = 3, @sortColumnName = 'LastName'

RESULT:
Data Paging by CTE dynamic Sort Column 2

Nested Common Table Expressions (i.e. CTE) – Sql Server

This is the fourth article in the series of articles on Common Table Expression in Sql Server. Below are the other articles in this series:

Introduction to Common Table Expression (a.k.a CTE)
Recursive CTE
Multiple CTEs in a Single Query

Nested Common Table Expressions

Nested CTEs is a scenario where one CTE references another CTE in it.

EXAMPLE 1: Below is a basic example of a Nested CTE:

Nested CTES

WITH FirstCTE 
   AS (SELECT 1 EmployeeId, 'Shreeganesh Biradar' Name)
, SecondCTE 
   AS (SELECT EmployeeId, Name, 'India' Country FROM FirstCTE)
SELECT *   FROM SecondCTE

RESULT:
Nested CTES result ver2

EXAMPLE 2: Below is another example of a Nested CTE:

This example uses the Employees table created in the previous article Introduction to Common Table Expression (a.k.a CTE). You can visit the link to create the table, if you have not created it already.

WITH FirstCTE AS 
   (SELECT Id EmployeeId, Name, ManagerId 
	FROM dbo.Employees WITH(NOLOCK) 
	WHERE ManagerId is NULL)
,  	SecondCTE AS
	(SELECT E.Id EmployeeId, E.Name, E.ManagerId, 
	        FCTE.Name ManagerName
	 FROM dbo.Employees E WITH(NOLOCK)
		INNER JOIN FirstCTE FCTE
		 ON E.ManagerId = FCTE.EmployeeId)
SELECT * FROM SecondCTE

RESULT:
Nested CTE Result 2

Multiple CTEs in a Single Query – Sql Server

This is the third article in the series of articles on Common Table Expression in Sql Server. Below are the other articles in this series:

Introduction to Common Table Expression (a.k.a CTE)
Recursive CTE
Nested Common Table Expressions

Multiple CTEs in a Single Query

Many a times we come across a scenario, where we need to use multiple CTEs in a single query. For instance assume first CTE gives employee details and second CTE gives total salary paid to the employee year to date after performing all the aggregation of the salary paid from the beginning of the year to till date. So, here combining the first CTE with the Second CTE in a single query gives a meaning ful details of employee with his year to date salary.

Below is very basic example of using multiple CTEs in a single query:

Multiple CTES in a Single query Ver2

WITH FirstCTE AS(SELECT 1 EmployeeId,'Shreeganesh Biradar' Name)
	, SecondCTE AS (SELECT 1 EmployeeId, '$ 100000' YTDSalary)
SELECT FC.EmployeeId, FC.Name, SC.YTDSalary
FROM FirstCTE FC
		INNER JOIN SecondCTE SC
		ON FC.EmployeeId = SC.EmployeeId

RESULT:
Multiple CTES in a Single query Result Ver2