Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Stored Procedure: Conditional Return

Hi I want to create a simple stored proecudre which does the following:

Psudocode

@tempSelect = "SELECT * FROM Table"

if (@Param is NULL) then
 exec @tempSelect
else
 exec @tempSelect + ' WHERE id = ' + @Param + '

Is this method efficent? Thank you.

like image 217
DavidS Avatar asked Sep 26 '26 05:09

DavidS


1 Answers

Try

select *
from table
where id=isnull(@param, id)
like image 95
John MacIntyre Avatar answered Sep 28 '26 17:09

John MacIntyre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!