Simple CASE or searched CASE statementĪs a rule of thumb, use a searched CASE statement when you want to execute a sequence of statements based on the results of multiple Boolean expressions and use a simple CASE statement when you want to execute a sequence of statements based on the result of a single expression. The ELSE statement clause will also never execute. Therefore, in this example, PL/SQL will never evaluate the last two conditions in the CASE statement. PL/SQL stops evaluating the subsequent condition once it finds the first condition that evaluates to TRUE.
However, if you do so, PL/SQL will implicitly use the following: If no values in WHERE clauses match the result of the selector in the CASE clause, the sequence of statements in the ELSE clause executes.īecause the ELSE clause is optional, you can skip it. In addition, the subsequent selector values are not evaluated. If the result of a selector value equals the result of the selector, then the associated sequence of statements executes and the CASE statement ends. The selector values i.e., selector_value_1, selector_value_2, etc., are evaluated sequentially. The result of the selector is used to select one of the several alternatives e.g., selector_value_1 and selector_value_2.
The selector is an expression which is evaluated once. Let’s examine the syntax of the simple CASE statement in detail:
Sql server case statement code#
Code language: SQL (Structured Query Language) ( sql )