Two ways
1. (example)
declare @col2 int
set @col2 = NULL ;
with a as
(select 1 as a
union all
select 2 as a)
select *
from a
left outer join
(select 1 as a, 'a' as b) as b on a.a=b.a
where
a.a =( case when @col2 is null then a.a else b.a end)
so,as per solution 1 set your query like this
select col1
from table1
left outer join
table2 on table1.col1 =table2.col1
where table1.col1 = (case when @col2 is null then table1.col1 else table2.col1 end)
2.
if (@col2 is null)
begin
select col1
from table1
left outer join
table2 on table1.col1=table2.col1
end
else
begin
select col1
from table1
join
table2 on table1.col1=table2.col1
End
Happy Coding!
:)