Hi.
Can anyone recommend the best way to handle this situation? I have a stored procedure that returns a list of properties. I also have optional parameters that can be passed, for example, if a state parameter is not passed, it is assumed that the user wants to see all properties, if a state parameter is passed, it is assumed the user only wants to see properties within that state. So far, I have come up with the following solutions. Can anyone tell me which one is better or recommend something different? Thanks!!
OPTION1
create procedure sp_GetProperties
@state char(2) = null,
@city_id int = null
as
select *
from property p
where (@state is null or @state = p.state) and
(@city_id is null or @city_id = p.city_id)
OPTION2
create procedure sp_GetProperties
@state char(2) = null,
@city_id int = null
as
select *
from property p
where p.state = case when @state is null then p.state else @state end and
p.city_id = case when @city_id is null then p.city_id else @city_id end
Can anyone recommend the best way to handle this situation? I have a stored procedure that returns a list of properties. I also have optional parameters that can be passed, for example, if a state parameter is not passed, it is assumed that the user wants to see all properties, if a state parameter is passed, it is assumed the user only wants to see properties within that state. So far, I have come up with the following solutions. Can anyone tell me which one is better or recommend something different? Thanks!!
OPTION1
create procedure sp_GetProperties
@state char(2) = null,
@city_id int = null
as
select *
from property p
where (@state is null or @state = p.state) and
(@city_id is null or @city_id = p.city_id)
OPTION2
create procedure sp_GetProperties
@state char(2) = null,
@city_id int = null
as
select *
from property p
where p.state = case when @state is null then p.state else @state end and
p.city_id = case when @city_id is null then p.city_id else @city_id end