Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Towards the innovative SQL ideas
Browse by Tags · View All
ms sql server 119
ms sql 118
sql server 116
sql 115
database 102
tsql 81
#SQL Server 78
t-sql 75
#sql 71
sql server general 67

Archive · View All
April 2011 14
July 2011 12
May 2011 12
August 2011 11
June 2011 10
September 2011 8
December 2011 6
November 2011 6
June 2013 5
April 2013 5

SQL Pagination with Order By Clause Enhancement - SQL Server Denali

Apr 28 2011 12:00AM by Paresh Prajapati   

In earlier version of SQL server, when we need to SQL pagination then we were using temp table, Common Table expression or Ranking functions. One best feature introduced by SQL Server 2011 and that enhancement is with ORDER BY clause. Yes this is the feature introduced by SQL Server Denali and we can use this option to get the pagination..

We can specify the Order by clause with OFFSET and FETCH NEXT keywords. Definition looks as following.

OFFSET : Determine the start row to begin on.
FETCH NEXT : How many rows need to fetch.

Let's look on some examples of ORDER BY clause with OFFSET and FETCH NEXT. Creating Database and table objects.
CREATE DATABASE PaginationDB
GO

use PaginationDB
GO

IF (OBJECT_ID('PaginationTable') > 0)
DROP TABLE PaginationTable
GO

CREATE table PaginationTable
(
PageId INT,
PageName varchar(100)
)
GO
Creating Sequence
IF EXISTS (SELECT 1 FROM SYS.SEQUENCES WHERE NAME = N'NewSequence')
DROP SEQUENCE NewSequence;
GO

CREATE SEQUENCE [NewSequence] AS INT
START with 1
INCREMENT by 1
MAXVALUE 1000
GO
Inserting records using sequence and looks for inserted records.
INSERT INTO PaginationTable
SELECT next value for [NewSequence], name FROM sys.objects
GO

SELECT PageId,PageName FROM PaginationTable
GO

Let us fetch  records from table using OFFSET and FETCH NEXT
-- Fetch first 10 rows
DECLARE @PageIndex INT = 1, @Pagesize INT = 10

SELECT PageId, PageName FROM PaginationTable
ORDER BY PageId 
OFFSET (@PageIndex - 1) * @Pagesize ROWS
FETCH NEXT @Pagesize ROWS ONLY
GO

-- Fetch next 10 rows
DECLARE @PageIndex INT = 2, @Pagesize INT = 10

SELECT PageId, PageName FROM PaginationTable
ORDER BY PageId 
OFFSET (@PageIndex - 1) * @Pagesize ROWS
FETCH NEXT @Pagesize ROWS ONLY
GO

-- Fetch Last rows
DECLARE @PageIndex INT = 1, @Pagesize INT = 10
DECLARE @TotalRows INT = (SELECT COUNT(1) FROM PaginationTable )

SELECT PageId, PageName FROM PaginationTable
ORDER BY PageId 
OFFSET (@TotalRows / @Pagesize ) * @Pagesize ROWS
FETCH NEXT @Pagesize ROWS ONLY
GO

I hope you already started the method of SQL paginataion with this new enhancement of Order By clause .

Tags: sql, tsql, sql server, ms sql, ms sql server, t-sql, new features, feature, sqll server 2011, sql server denali, #SQL Server, mssql,


Paresh Prajapati
6 · 22% · 7044
3
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

Your Comment


Sign Up or Login to post a comment.

"SQL Pagination with Order By Clause Enhancement - SQL Server Denali" rated 5 out of 5 by 3 readers
SQL Pagination with Order By Clause Enhancement - SQL Server Denali , 5.0 out of 5 based on 3 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]