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 Methods in SQL Server - which method you like?

May 9 2011 12:00AM by Paresh Prajapati   

In my earlier post i have wrote about new feature  "Order By Clause Enhancement". I have given demonstration with examples as well as how can we use this feature.

It is used for SQL pagination and also explained for the same for pagination.

Here i want to give same demonstration but not with only Order By Clause enhancement, It will be various ways and methods for SQL pagination.  We will look for the native method and new method of SQL pagination and review it. 
USE PaginationDB
GO

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

CREATE table PaginationTable
(
PageId INT,
PageName VARCHAR(100)
)
GO

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 1000000
GO
Inserting records using sequence and looks for inserted records.
INSERT INTO PaginationTable
SELECT next value for [NewSequence], Name FROM sys.objects
GO 5000
We will fetch 10 records using various methods, Using TempTable :
CREATE TABLE #PaginationTemp
(
Id INT IDENTITY(1,1),
PageId INT,
PageName VARCHAR(100)
)

INSERT INTO #PaginationTemp(PageId, PageName)
SELECT 
PageId, 
PageName 
FROM PaginationTable
ORDER BY PageName,PageId

DECLARE @PageIndex INT = 1, @Pagesize INT = 10

SELECT * FROM #PaginationTemp
WHERE Id BETWEEN (@PageIndex - 1) * @Pagesize AND ((@PageIndex - 1) * @Pagesize) + @Pagesize

GO
Using query alias: 
DECLARE @PageIndex INT = 1, @Pagesize INT = 10

SELECT res.PageId, res.PageName
FROM
(
SELECT 
ROW_NUMBER() OVER(ORDER BY PageName,PageId) as RW,
PageId, 
PageName 
FROM PaginationTable
) res
WHERE RES.RW BETWEEN (@PageIndex - 1) * @Pagesize AND ((@PageIndex - 1) * @Pagesize) + @Pagesize

GO
Using Common Table Expression :
DECLARE @PageIndex INT = 1, @Pagesize INT = 10

;With CTEPage
AS
(
SELECT 
ROW_NUMBER() OVER(ORDER BY PageName,PageId) as RW,
PageId, 
PageName 
FROM PaginationTable
) 

SELECT PageId, PageName FROM CTEPage
WHERE RW BETWEEN (@PageIndex - 1) * @Pagesize AND ((@PageIndex - 1) * @Pagesize) + @Pagesize

GO
Using OFFSET and FETCH NEXT and Order by introduced by SQL Server Denali :
DECLARE @PageIndex INT = 1, @Pagesize INT = 10

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

GO
I got one suggestion from Jacob(blog) , and he had a good and optimized suggestion for SQL Pagination as we should put TOP clause in script same as page size, so it will filter rows as per search criteria. Like we have script using TOP clause and revised script is as following,
DECLARE @PageIndex INT = 1, @Pagesize INT = 10

INSERT INTO #PaginationTemp(PageId, PageName)
SELECT 
TOP (((@PageIndex - 1) * @Pagesize) + @Pagesize)
PageId, 
PageName 
FROM PaginationTable
ORDER BY PageName,PageId

SELECT * FROM #PaginationTemp
WHERE Id BETWEEN (@PageIndex - 1) * @Pagesize AND ((@PageIndex - 1) * @Pagesize) + @Pagesize

GO
Here you have same output for all above such methods including last optimized suggested by Jacob used for SQL pagination.

You can share your ideas if you have a new methods for pagination.

Tags: sql, sql server 2008, sql server 2005, tsql, sql server, ms sql, ms sql server, t-sql, new features, feature, SET, sql server denali, #SQL Server, mssql, #sql, sql server 2011, pagination,


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



Submit

Your Comment


Sign Up or Login to post a comment.

"SQL Pagination Methods in SQL Server - which method you like?" rated 5 out of 5 by 2 readers
SQL Pagination Methods in SQL Server - which method you like? , 5.0 out of 5 based on 2 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]