Try the following:
select t1.name,t1.bday,t2.address,t2.contactnum from table1 as t1 left join table2 as t2 on t1.p_id = t2.p_id where (@qualified = '2' AND t2.role is null) OR (@qualified = '3' AND t2.role is not null)
I believe this syntax is a conditional expression that you tried to implement. However, such a WHERE may lead to performance problems. If this happens you should use:
IF @qualified = '2' THEN BEGIN select t1.name,t1.bday,t2.address,t2.contactnum from table1 as t1 left join table2 as t2 on t1.p_id = t2.p_id where t2.role is null END IF @qualified = '3' THEN BEGIN select t1.name,t1.bday,t2.address,t2.contactnum from table1 as t1 left join table2 as t2 on t1.p_id = t2.p_id where t2.role is not null END
source share