Do all parts of a SQL SERVER expression get using "OR"?

Given:

WHERE (@Id Is NULL OR @Id = Table.Id) 

If @Id is null: expression evaluates to true. The second part @Id = Table.Id is still being considered? or enough that the expression is evaluated as true, given that the first part (which is the case in C #).

This is true because of some of the more complex OR statements, where it is important to know if all the details are evaluated.


UPDATE:

Since then I have found this syntax to be a good alternative.

 WHERE (Table.Id = ISNULL(@Id, Table.Id)) 
+4
source share
2 answers

Sometimes it is, sometimes it is not. SQL Server does not guarantee a short circuit of the boolean statement; do not rely on it for correctness. See this blog post: Boolean Short-circuited Operator .

Complex queries dependent on @variables like this are much better written as explicit IF statements:

 IF (@id IS NULL) SELECT ... FROM ... WHERE ... ELSE SELECT ... FROM ... WHERE ... 
+9
source

Implementation plans may not be so big with such a request. Both will be appreciated.

+1
source

All Articles