1

I have this stored procedure:

ALTER PROCEDURE [dbo].[GetCalendarEvents]
    (@StartDate datetime, 
     @EndDate datetime, 
     @Location varchar(250) = null)
AS
BEGIN
    SELECT * 
    FROM Events 
    WHERE EventDate >= @StartDate 
      AND EventDate <= @EndDate 
      AND (Location IS NULL OR Location = @Location)
END

Now, I have the location parameter, what I want to do is if the parameter is not null then include the parameter in where clause. If the parameter is null I want to completely ignore that where parameter and only get the result by start and end date.

Because when I'm doing this for example:

EXEC GetCalendarEvents '02/02/2014', '10/10/2015', null

I'm not getting any results because there are other locations which are not null and since the location parameter is null, I want to get the results from all the locations.

Any idea how can I fix this?

4 Answers 4

2
ALTER PROCEDURE [dbo].[GetCalendarEvents]
( @StartDate DATETIME,
  @EndDate DATETIME,
  @Location VARCHAR(250) = NULL
)
AS
BEGIN
    SELECT  *
    FROM    events
    WHERE   EventDate >= @StartDate
            AND EventDate <= @EndDate
            AND Location = ISNULL(@Location, Location )
END

If a NULL column is a possibility, then this would work.

ALTER PROCEDURE [dbo].[GetCalendarEvents]
( @StartDate DATETIME,
  @EndDate DATETIME,
  @Location VARCHAR(250) = NULL
)
AS
BEGIN
    IF ( @loc IS NULL )
        BEGIN
            SELECT  *
            FROM    events
            WHERE   EventDate >= @StartDate
                    AND EventDate <= @EndDate
        END
    ELSE
        BEGIN
            SELECT  *
            FROM    events
            WHERE   EventDate >= @StartDate
                    AND EventDate <= @EndDate
                    AND Location = @Location
        END
END

As having an 'OR' clause should be reasonably avoided due to possible performance issues.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I'll accept your answer in 10 minutes. That did it. Have a good weekend
This will fail if the Location column in the events table can contain null. The advice from @TT. is better.
0

The part in the WHERE clause should then read

AND (@Location IS NULL OR Location=@Location)

Comments

0

Try this

 SELECT * 
    FROM Events 
    WHERE EventDate >= @StartDate 
      AND EventDate <= @EndDate 
      AND Location = Case When LEN(@Location) > 0 Then  @Location Else Location End

Comments

0

It can be easily done with a dynamic sql query.

ALTER PROCEDURE [dbo].[GetCalendarEvents]
(@StartDate datetime, 
 @EndDate datetime, 
 @Location varchar(250) = null)
AS
BEGIN
   DECLARE @SQL NVARCHAR(MAX);
   DECLARE @PARAMETER_DEFIINITION NVARCHAR(MAX);
   DECLARE @WHERE_PART NVARCHAR(MAX);

   SET @PARAMETER_DEFIINITION =' @StartDate DATETIME, @EndDate DATETIME, @Location VARCHAR(250) '

   SET @SQL ='SELECT * 
              FROM Events 
              WHERE EventDate >= @StartDate 
              AND EventDate <= @EndDate '

   IF @Location IS NOT NULL
   BEGIN
       SET @WHERE_PART = ' AND Location  = @Location '
   END    

   SET @SQL = @SQL + @WHERE_PART
   EXEC SP_EXECUTESQL @SQL, @PARAMETER_DEFIINITION, @StartDate, @EndDate, @Location
END

Query will be dynamically created according to the parameters. In here if @location is null then it will not add to the where part.

If you want more on writing dynamic queries please refer this article. http://codingpulse.blogspot.com/2015/02/dynamic-sql-in-stored-procedure-part-1.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.