Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4068062
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T16:19:42+00:00 2026-05-20T16:19:42+00:00

declare @PageIndex int declare @PageSize int declare @CategoryID int declare @FromReleaseDate datetime declare @TillRelaseDate

  • 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>=@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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T16:19:43+00:00Added an answer on May 20, 2026 at 4:19 pm

    Using table variable…

    declare @filtered as table (
          ID int
        , CategoryID int
    )
    
    insert into @filtered
      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
    
    with filtered as (
        select * from @filtered
    )
    ... Rest of the query
    
    select * from @filtered
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

declare @servername varchar(2000) set @EmriServerit=(select @@servername) declare @dbname varchar(2000) set @dbname ='Test1' declare @Dir
declare @top int set @top = 5 select top @top * from tablename Is
declare @customerID int set @customerID=1 ;with cteDates as ( select dateadd(hh, datediff(hh, 0, getdate()),
declare @a varchar(40) set @a='1.23e-4' declare @b decimal(27,12) if isnumeric(@a) =1 begin select @b=cast(@a
declare @dateto as datetime set @dateto='8/1/11' SELECT ti.userkey, SUM(l.Price * l.OrderQty) AS SumOfOrders, count
declare @data datetime set @data = '2011-01-01 23:59:59:999' select @data result is: 2011-01-02 00:00:00.000
DECLARE @TotalMaxQty int SET @TotalMaxQty = 1000 SELECT PKId ,Qty FROM dbo.Sales PKId Qty
DECLARE @table table(XYZ VARCHAR(8) , id int) INSERT INTO @table SELECT '4000', 1 UNION
declare @d varchar set @d = 'No filter' if (@d like 'No filter') BEGIN
declare fName varchar2(255 char); begin SELECT x.constraint_name into fName FROM all_constraints x JOIN all_cons_columns

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.