>So, what do you pass for all master rows? Suppose 0 means give me all Master records, your SQL will be
>
>select TW.Numero from Teamwork TW where @Param = 0 OR exists (select 1 from TeamWorkMember TWM where TWM.NoTeamWork = TW.Numero and TWM.NoMember = @Param) OPTION (recompile)
>
>OPTION (RECOMPILE) is important and will produce good plan in case you're using SQL 2008 R2 and up.
>
>Otherwise you may want to create dynamic SQL.
Thanks, I shall take a look at that. For now, I have simply put a condition to include this OUTER APPLY only when needed.