The following is my SP:
Alter PROCEDURE GetList
(
@FromDate date = null,
@ToDate date = null
)
AS
Select * FROM CallList c
Where c.CallDate > @FromDate and c.CallDate < @ToDate
If there was no passed date filter, I want to get all the records.
How would I do it?
A couple of viable options:
You could set
@FromDateand@ToDateto be equal to a very early or very late date respectively they’re NULL.You could use sp_executesql and construct a dynamic query string w/ parameters as needed e.g.
This latter approach performs better than using ORs all over the place, as queries that include ORs invariably end up getting optimized very badly – they might work well for a certain set of parameters but are generally not one-size-fits-all.