I would like auto append the where clause depending on the values supplied
Let us consider a stored procedure that contains a select query
CREATE PROCEDURE [dbo].[example]
@From DATETIME,
@T0 DATETIME
AS
BEGIN TRY
SELECT *
FROM footable
WHERE Fromdate = @From AND Todate = @TO
END TRY
If the value of @From is null then the where clause must be
SELECT *
FROM footable
WHERE Todate = @TO
Is there any way to implement this ?
Thanks a lot for the answer. but this does not solve my problem either because @From
and @To
both are optional. Following this approach my code would like
SELECT *
FROM footable
WHERE (Fromdate = @From OR @From IS NULL)
AND (Todate = @TO OR @To IS NULL)
Executing this results in the entire table.