Talay
Talay

Reputation: 381

T-SQL: Need help understanding 'Strange' SQL in Where Clause

I have the following code in the Where Clause of an otherwise straightforward Query that I do not understand and would appreciate help with:

    ... AND  --Filter on StoreID
(
('545' IS NOT NULL AND a.StoreID IN (545)) OR
('545' IS NULL)
)
AND ...

The query appears to return the exact same results if I change it to:

... AND a.StoreID IN (545) AND ...

I would appreciate any help with understanding this.

Thanks, in advance!!

Upvotes: 0

Views: 58

Answers (1)

earloc
earloc

Reputation: 2090

It looks as if the value of '545' was passed via a parameter, mostly used in stored procedures or via dynamic SQL generation from an application:

(@value IS NOT NULL AND a.StoreID IN (@value)) OR
(@value IS NULL)

This kind of clause would do a IN search, if @value is not NULL, or select "all" records, when @value was NULL.

In general, this would conditionally filter the results if a value was supplied

Upvotes: 2

Related Questions