WHEN 'C' THEN dbms_output.put_line('Good') WHEN 'B' THEN dbms_output.put_line('Very Good') WHEN 'A' THEN dbms_output.put_line('Excellent') Let us rewrite the preceding code using the CASE statement, as follows: In each instance, we test whether the same variable, grade, is equal to one of five values: 'A', 'B', 'C', 'D', or 'F'. To compare the IF and CASE statements, consider the following code that outputs descriptions of school grades: However, to select the sequence, the CASE statement uses a selector rather than multiple Boolean expressions. Like the IF statement, the CASE statement selects one sequence of statements to execute. When the first condition is true, its associated statement is executed and control passes to the INSERT statement. Nevertheless, bonus is assigned the proper value of 1500 because the section is never tested. ![]() If the value of sales is larger than 50000, the first and second conditions are true. INSERT INTO payroll VALUES (emp_id, bonus. If all conditions are false or null, the sequence in the ELSE clause is executed. ![]() If any condition is true, its associated sequence of statements is executed and control passes to the next statement. Conditions are evaluated one by one from top to bottom. An IF statement can have any number of ELSIF clauses the final ELSE clause is optional. If the first condition is false or null, the ELSIF clause tests another condition. The third form of IF statement uses the keyword ELSIF (not ELSEIF) to introduce additional conditions, as follows: Sometimes you want to select an action from several mutually exclusive alternatives. That is, IF statements can be nested, as the following example shows: The THEN and ELSE clauses can include IF statements. UPDATE accounts SET balance = balance - debit WHERE. UPDATE accounts SET balance = balance + credit WHERE. In the following example, the first UPDATE statement is executed when the condition is true, but the second UPDATE statement is executed when the condition is false or null: Thus, the ELSE clause ensures that a sequence of statements is executed. The sequence of statements in the ELSE clause is executed only if the condition is false or null. The second form of IF statement adds the keyword ELSE followed by an alternative sequence of statements, as follows: You might want to place brief IF statements on a single line, as in UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id In either case, control passes to the next statement. ![]() If the condition is false or null, the IF statement does nothing. The sequence of statements is executed only if the condition is true. The simplest form of IF statement associates a condition with a sequence of statements enclosed by the keywords THEN and END IF (not ENDIF), as follows: The CASE statement is a compact way to evaluate a single condition and choose between many alternative actions. There are three forms of IF statements: IF-THEN, IF-THEN-ELSE, and IF-THEN-ELSIF. That is, whether the sequence is executed or not depends on the value of a condition. In the examples under the topic The Difference Between DECODE and CASE, identify which use simple CASE and which use searched CASE.The IF statement lets you execute a sequence of statements conditionally. The searched CASE expression is the Oracle 8i variant. Not that it matters but for the trivia buffs (and also if you’re working on an old version of Oracle) – the simple CASE expression was introduced in Oracle 9i. In the above, though employee CLARK may match two conditions, only process_highpaid(empno) will be executed. The searched CASE has the following structure: case The simple CASE has the following structure: Let’s have a closer look to compare simple CASE and searched CASE in structure and functionality.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |