Can I use a previous OUTER APPLY in a subsequent OUTER APPLY

Bobby P 221 Reputation points
2022-08-12T19:34:33.743+00:00

So I have an [OuterApply1] which SELECTs TOP (1) from a ROW_NUMBER. I would then like to use that [OuterApply1] in another subsequent [OuterApply2] to filter based on what is returned in the [OuterApply1].

Is this possible?

Can I do that?

Thanks for your review and am hopeful for a reply.

Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,601 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Erland Sommarskog 107.2K Reputation points
    2022-08-12T21:33:26.957+00:00

    It would have helped if you had a supplied an example to clarify what you are talking about. But, yes, you can do:

       SELECT ...  
       FROM  tbl t  
       OUTER APPLY (...) AS A  
       OUTER APPLY (SELECT that refers to A here) AS B  
    

    `Although, I am not fully sure that is what you have in mind.

    0 comments No comments