Currently I have a stored proc, which takes 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 inline the if statement, thereby not declaring 2 select's just because of 1 param?
For small items with two outcomes, I typically use IIF statements (i.e. If x < a, then 1, else 0) but for longer drawn out statements that can have multiple outcomes, I use CASE.
The CASE is slightly faster than IIF. IF is a control of flow statement; it indicates which T-SQL statement to evaluate next, based on a condition. CASE is a function -- it simply returns a value.
If != and <> both are the same, which one should be used in SQL queries? Here is the answer – You can use either != or <> both in your queries as both technically same but I prefer to use <> as that is SQL-92 standard.
SELECT * FROM TABLE WHERE (@vari is null or col = @vari)
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