Case statements and case expressions are an important part of plsql Process Control. Although they are easy to use, they are easy to confuse. This article describes the case statement and case expression, and provides a demonstration to better understand the similarities and differences between the two and the precautions for use.
I. Simple case statements
- --> Syntax
-
- CASESELECTOR
- WHENEXPRESSION 1THENSTATEMENT 1;
- WHENEXPRESSION 2THENSTATEMENT 2;
- ...
- WHENEXPRESSION NTHENStatement n;
- ELSEStatement n + 1;
- END CASE;
-
- -- A simple case statement indicates that the value or result obtained in SELECTOR matches the value or result in EXPRESSION n. Once a match is found, the corresponding statement is executed. Until it is found.
- -- If EXPRESSION n does not match, then the ELSE runs the corresponding statement.
- -- ELSE is optional. If any matching item is omitted and no matching item is found in the when condition, the case_not_found exception is received.
-
- --> Demonstrate simple case statements
- Sys @ ORCL>DECLARE
- 2 v_num NUMBER: = & in_num;
- 3 v_flag NUMBER;
- 4BEGIN
- 5 v_flag: = MOD (v_num, 2 );
- 6
- 7CASEV_flag
- 8WHEN0
- 9THEN
- 10 DBMS_OUTPUT.put_line (v_num |'Is even number');
- 11WHEN1
- 12THEN
- 13 DBMS_OUTPUT.put_line (v_num |Is odd number');
- 14ELSE
- 15NULL;
- 16END CASE;
- 17END;
- 18/
- Enter valueForIn_num: 5
- 5IsOdd number
-
- PL/SQLProcedureSuccessfully completed.
Ii. Search-type case statement
- --> Syntax
-
- CASE
- WHENSearch condition 1THENSTATEMENT 1;
- WHENSearch condition 2THENSTATEMENT 2;
- ...
- WHENSEARCH CONDITION NTHENStatement n;
- ELSEStatement n + 1;
- END CASE;
-
- -- The search-type case statement looks different from the simple case statement. First, there is no selector after case, and then the result of search condition n after when is boolean,
- -- The result of the first true statement is returned. If the search condition after all when is not TRUE, the STATEMENT after else
- -- Is returned. If the else clause is omitted at this time, it is equivalent to a simple case statement and a case_not_found exception is also received. Who told them to be a compatriot?
-
- --> The following example shows a search-type case.
- Scott @ ORCL>DECLARE
- 2 v_num NUMBER: = & in_num;
- 3BEGIN
- 4CASE
- 5WHENV_num> 0
- 6THEN
- 7 DBMS_OUTPUT.put_line (v_num |'Is a positive number');
- 8WHENV_num <0
- 9THEN
- 10 DBMS_OUTPUT.put_line (v_num |'Is a negative number');
- 11ELSE
- 12 DBMS_OUTPUT.put_line (v_num |Is zero');
- 13END CASE;
- 14END;
- 15/
- Enter valueForIn_num:-3
- -3IsA negative number
-
- --> As shown in the following example, if none of the when values is true and the else clause is omitted, CASE not found is welcome.
- Scott @ ORCL>DECLARE
- 2 v_num NUMBER: = & in_num;
- 3BEGIN
- 4CASE
- 5WHENV_num> 0
- 6THEN
- 7 DBMS_OUTPUT.put_line (v_num |'Is a positive number');
- 8WHENV_num <0
- 9THEN
- 10 DBMS_OUTPUT.put_line (v_num |'Is a negative number');
- 11END CASE;
- 12END;
- 13/
- Enter valueForIn_num: 0
- DECLARE
- *
- ERRORAtLine 1:
- ORA-06592:CASE NotFound while executingCASEStatement
- ORA-06512:AtLine 4