views:

35

answers:

2

I have the following query that returns a set of rows based on some input parameters :

  WITH hierarchy AS (
     SELECT yt.id    
       FROM [dbo].[TH_Categories] yt
      WHERE yt.ID = @topicID And CultureID = @cultureID 
     UNION ALL
     SELECT yt.id    
       FROM [dbo].[TH_Categories] yt
       JOIN hierarchy h ON h.ID = yt.ParentCategoryID)
    Select id, 
           [SiteName]
          ,[SiteURL]
          ,[Description]
          ,[Logo]
          ,[CultureID]
          ,[DateAdded],  
          dbo.GetSiteFollowers(id) AS Followers 
     from dbo.TH_Sites 
    where id in (Select Distinct SiteID 
                   from dbo.TH_CategoryFeeds 
                  Where CatID in (SELECT ID
                                    FROM hierarchy t ))

I am trying to implement paging using ROW_Number() function, but I am not able to find the way.

Any Help would be Highly appreciate it.

A: 

See SQL Server 2005 Paging – The Holy Grail for a good discussion of paging techniques.

Joe Stefanelli
I need to register to read the whole article :).
Joseph Ghassan
Registration is free and [SQLServerCentral.com](http://www.sqlservercentral.com/) is an excellent resource in general. It's well worth the 30 seconds it will take to register.
Joe Stefanelli
+1  A: 

try this:

;WITH hierarchy AS 
(
     SELECT yt.id    
       FROM [dbo].[TH_Categories] yt
      WHERE yt.ID = @topicID And CultureID = @cultureID 
     UNION ALL
     SELECT yt.id    
       FROM [dbo].[TH_Categories] yt
       JOIN hierarchy h ON h.ID = yt.ParentCategoryID
)
, YourRows AS 
(
    Select id, 
           [SiteName]
          ,[SiteURL]
          ,[Description]
          ,[Logo]
          ,[CultureID]
          ,[DateAdded]
          ,dbo.GetSiteFollowers(id) AS Followers 
          ,ROW_NUMBER() OVER (ORDER BY ActivityDate DESC) AS RowNumber
     from dbo.TH_Sites 
    where id in (Select Distinct SiteID 
                   from dbo.TH_CategoryFeeds 
                  Where CatID in (SELECT ID
                                    FROM hierarchy t ))
)
SELECT * FROM YourRows 
WHERE RowNumber>=@Start and RowNumber<=@End
KM
You are GOD. thanks man.
Joseph Ghassan