ali raza

C#, .Net ,Asp.net and my thoughts

database level paging in Tsql 2005 with common table expression

In MSSQL 2000 we used to do paging either by dynamic sql or by some
 advanced techniques like the example with rowcount.
In MSSQL 2005 with the introduction of ROW_NUMBER
 function life is a lot easier.   

 
DECLARE @PageNum AS INT;
DECLARE @PageSize AS INT;
SET @PageNum = 2;
SET @PageSize = 10;
 WITH OrdersRN AS (     SELECT ROW_NUMBER() OVER(ORDER BY OrderDate, OrderID) AS RowNum           ,
OrderID ,
OrderDate,
CustomerID,
EmployeeID
FROM dbo.Orders )
SELECT *    FROM OrdersRN  WHERE RowNum BETWEEN (@PageNum - 1) * @PageSize + 1 AND @PageNum * @PageSize  ORDER BY OrderDate,
OrderID;
 

One response to “database level paging in Tsql 2005 with common table expression

  1. Ruba Ardah August 16, 2007 at 7:21 am

    Hi every body,

    Really I need your help to pass my exam 070-431, that I failed this exam because all questions in this exam were from old version exam, that contains 15 simulations, these simulations about backups indexes and linked server ,so if any body can help me to find these 15 simulation questions , it will help me to pass the exam

    Thanks for your concern
    Ruba

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: