I have a table valued function
CREATE FUNCTION [dbo].[DateRange]
(
@StartDate date,
@EndDate date,
@Location varchar(25),
@Device varchar(25)
)
RETURNS TABLE
AS
RETURN
(
SELECT *
FROM MyTable
WHERE Date < @EndDate AND
Date > @StartDate AND
Location = @Location AND
Device = @Device
);
Now I would like to give the user the option to use NULL as location parameter and or device parameter so he/she gets all data independent of the location and device back
What would be the most elegant and efficient way to do this ?
Best Answer
Like so :
IF the
device
andlocation
columns can actually be NULL, you might want to consider this option though:This would work if you're using a UD table type for the location, as you asked about in your comment. Just make sure the input parameter is Read Only. However, you cannot pass NULL into a table type parameter, you have to pass in an empty parameter object of that type :
Call it: