I created a SQL stored procedure:
CREATE PROCEDURE [stored procedure name]
(
@Location [varchar](255),
@start_date datetime,
@end_date datetime
)
AS
BEGIN
SELECT id, location, name, begin_date, age, career, phone_number
FROM information
WHERE (begin_date = @start_date OR @start_date IS NULL)
AND (begin_date = @end_date OR @end_date IS NULL)
AND (location = @Location OR @Location = 'All')
END
My thought is to set @start_date and @end_date default to Null but it can also show specific data from a period of time if user select to do so, which means:
- When I run the following code:
EXEC [stored procedure name] @start_date = NULL, @end_date = NULL, @Location = 'New York';
It will show all data from New York regardless of time.
- When I run the following code:
EXEC [stored procedure name] @start_date = '2020-03-01', @end_date = '2020-03-31', @Location = 'New York';
It will show data from New York during March 1st, 2020 to March 31st, 2020.
How can I adjust my code to achieve this goal? It seems that my code lacks a connection between @start_date and @end_date.