• Well subject to indexes and performance testing I do this

    ;with cte (ID) AS (

    SELECT ID

    FROM Test1 T1

    WHERE @p_flag = 1

    ANDT1.moveFlag = 26

    UNION ALL

    SELECT ID

    FROM Test1 T1

    WHERE @p_flag = 0

    ANDT1.moveFlag <> 26)

    SELECT ..

    FROM cte T1

    JOIN Test2 T2 ON T1.ID = T2.ID

    Far away is close at hand in the images of elsewhere.
    Anon.