Run order of searched CASE expression

Asked

Viewed 106 times

0

The CASE WHEN has two forms of functionally, with simple and researched expressions:

The expression SIMPLE CASE compares an expression with a set of simple expressions to determine the result.

The expression CASE researched evaluates a set of boolean expressions to determine the result.

In the documentation it is said that the CASE WHEN stops at the first satisfactory instruction, but in some situations the expression is evaluated first:

The CASE instruction evaluates its conditions in sequence and towards the satisfactory first condition. In some situations, an expression is assessed before a CASE instruction receives the results of expression as its input. It is possible that there are errors in the evaluation of these expressions. The aggregate expressions that appear in arguments WHEN of a CASE instruction are evaluated first and then are provided for the CASE instruction.

CASE - Transect SQL

I was left with doubt on how SQL executes the instructions in the order in which it reads the expressions, more specific in the example below:

CASE
   WHEN validado = 0 THEN 
    1
   WHEN (SUBQUERY COM FUNÇÃO DE AGREGAÇÃO) > 0 THEN
    2
   WHEN MAX(valor) > 0 THEN 
    3
END

If the first expression is true sql will not run subquery?

  • not... is why he would execute ? .

  • In some languages, if I remember correctly, if there is no break or Return all cases within a switch are checked. But if in the documentation it is informed that he stops at first he will not follow. If you need the others to be validated, I would recommend the same IF structure.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.