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>=@FromReleaseDate)
and (a.ReleaseOn <=@TillRelaseDate )
)
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
I have to return 2 tables. But I can’t do it, because filtered in the second query is not accessible. Is there any way to fix this error?
Using table variable…