Execure join on certain condition only
In my query i want inner join of on 'table3' to be executed based on 'statecode' and 'type' condition. To achieve that the below query1 is correct? or i need use IF condition as mentioned in Query2Query1:
select * from table1 t1
inner join table2 t2 on t1.id=t2.id
inner join table3 on t2.id=t3.id and statecod='A' and type=1
Query2:
if(@statecode='A' and type=1)
BEGIN
select * from table1 t1
inner join table2 t2 on t1.id=t2.id
inner join table3 on t2.id=t3.id
END
ELSE
BEGIN
select * from table1 t1
inner join table2 t2 on t1.id=t2.id
END