3

I'm sure someone can quickly solve this for me. I'm sure I'm not the first to ask this question, but i didn't find anything similar yet.

Having the following WHERE statement:

WHERE boolean_expression1 OR boolean_expression2 OR subquery_count > 0

Is this executed this way?:

  • if (boolean_expression1 = true) -> exit with true
  • else if (boolean_expression2 = true) -> exit with true
  • else -> execute subquery?

I mean: Is the subquery only executed if the preceeding expressions both are false? And if not, is there any way I can ensure this?

Because the subquery could be quite expensive in my case.

Thx in advance, Chris

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 3
    SQL Server does **NOT** make any promises about the order of evaluation - and you cannot force any order, either. – marc_s Jun 14 '13 at 08:39
  • How about CASE WHEN-expressions? Could you imagine a statement for this OR example: WHERE (@GlobalSearch = 1 OR @SomeCount = 0 OR (subquery) >= @SomeCount) – Christian Kahlau Jun 14 '13 at 08:43
  • 1
    check the execution plan. http://stackoverflow.com/questions/7359702/how-do-i-obtain-a-query-execution-plan – Eric213 Jun 14 '13 at 08:46

1 Answers1

0
WHERE case when boolean_expression1 then 1
else case when boolean_expression2 then 1
else when subquery_count > 0 then 1 end = 1
t-clausen.dk
  • 43,517
  • 12
  • 59
  • 92