select * from table1 t1 case when flag = 0 then left join else right join end table2 t2 on t2.id = t1.id
if @flag = 0 select * from table1 t1 left join table2 t2 on t2.id = t1.id else select * from table1 t1 right join table2 t2 on t2.id = t1.id
select * from table1 t1 full join table2 t2 on t2.id = t1.id where (flag = 0 and t2.id is null) or (flag <> 0 and t1.id is null)
select * from table1 t1 full join table2 t2 on t2.id = t1.id where (flag = 0 and t1.id is not null) or (flag <> 0 and t2.id is not null)
select * from table1 t1 left join table2 t2 on t2.id = t1.id where flag = 0 union all select * from table1 t1 right join table2 t2 on t2.id = t1.id where flag <> 0