0
declare @PageIndex int
declare @PageSize int
declare @CategoryID int
declare @FromReleaseDate datetime
declare @TillRelaseDate datetime
set @CategoryID =6
set @FromReleaseDate = '1.01.2000'
set @TillRelaseDate = '1.01.2022'
set @PageIndex =1
set @PageSize=2
begin
with filtered as (
select ArticleList.ID as ID, ArticleList.CategoryID as CategoryID
from (
select a.*, c.ID as cID, c.ParentID as ParentID,
ROW_NUMBER() over(order by ReleaseOn desc) as RowNum
from Article as a
inner join Category as c
on a.CategoryID=c.ID and (@CategoryID is null or a.CategoryID = @CategoryID )
where (a.ReleaseOn>[email protected])
and (a.ReleaseOn <[email protected])
)
as ArticleList
where ArticleList.RowNum between
(@PageIndex - 1) * @PageSize + 1 and @PageIndex*@PageSize
)
select c.* from Article as a
inner join Category as c on a.CategoryID=c.ID
where
c.id in (select CategoryID from filtered)
select a.*
from Article as a
inner join Category as c on a.CategoryID=c.ID
where a.id in (select id from filtered)
end
我必須返回2個表。但我做不到,因爲已過濾在第二個查詢中無法訪問。有沒有辦法解決這個錯誤?sql查詢 - 返回2表
開箱即用的,我能想到的臨時表的.... – 2011-03-17 18:29:16