Can I use a flag to select different WHERE clause? Something like this.
SELECT fields
from T3 t3
left outer join T1 t1 on T1.Id=t2.Id
WHERE
CASE
WHEN @Status <> 1
THEN (t3.Id = @SId AND sh.StatusId = 3 AND k.StatusId = 1)
ELSE t2.Id = @SID AND t2.StatusID = 12
GROUP BY whatever
preferred way:
WHERE
(@Status <> 1 AND t3.Id = @SId AND sh.StatusId = 3 AND k.StatusId = 1)
OR (t2.Id = @SID AND t2.StatusID = 12)
or this is a little easier to read case statement:
WHERE
CASE WHEN @Status <> 1
AND t3.Id = @SId
AND sh.StatusId = 3
AND k.StatusId = 1 THEN 1
WHEN t2.Id = @SID
AND t2.StatusID = 12 THEN 1
ELSE 0 END = 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With