I am working on optimizing some heavily used stored procedures and ran across a scenario that raised a question that I couldn't find any answers for: when evaluating TSQL in a stored procedure, does SQL Server short-circuit the IF
statement?
For example, assume a stored procedure has code similar to:
IF @condition1 = 1
OR EXISTS(SELECT 1 FROM table1 WHERE column1 = @value1)
...
In this scenario does SQL Server short-circuit the evaluation such that the EXISTS
statement is never executed when the preceding clause evaluates to true?
If it never or only sometimes does, then we have some rewriting ahead of us.
if
statements will do that. When I looked at this before I found some examples of it not short circuiting. You could also look into using case statements. – Superscription