我有以下查询,它根据一些输入参数返回一组行:
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 ))
我正在尝试使用ROW_Number()函数实现分页,但我无法找到方法。
任何帮助都会非常感激。
答案 0 :(得分:1)
试试这个:
;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
答案 1 :(得分:0)
有关分页技术的详细讨论,请参见S QL Server 2005 Paging – The Holy Grail。