Shorthand IF expressions in sql

I currently have a saved proc that accepts a string

@vari as varchar(30)
if @vari is null 
    SELECT * FROM TABLE 
else 
    SELECT * FROM TABLE WHERE col = @vari
endif

is there any way to embed the if statement, thereby not declaring 2 select just because of 1 param?

+5
source share
2 answers
SELECT * FROM TABLE WHERE (@vari is null or col = @vari)
+15
source

Assuming it is colnever NULL, you can do this:

select *
from table
where col = isnull(@vari, col)
+3
source

All Articles