Oracle Tutorial: CASE statement and CASE expression

Source: Internet
Author: User

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

  1. --> Syntax
  2. CASESELECTOR
  3. WHENEXPRESSION 1THENSTATEMENT 1;
  4. WHENEXPRESSION 2THENSTATEMENT 2;
  5. ...
  6. WHENEXPRESSION NTHENStatement n;
  7. ELSEStatement n + 1;
  8. END CASE;
  9. -- 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.
  10. -- If EXPRESSION n does not match, then the ELSE runs the corresponding statement.
  11. -- 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.
  12. --> Demonstrate simple case statements
  13. Sys @ ORCL>DECLARE
  14. 2 v_num NUMBER: = & in_num;
  15. 3 v_flag NUMBER;
  16. 4BEGIN
  17. 5 v_flag: = MOD (v_num, 2 );
  18. 6
  19. 7CASEV_flag
  20. 8WHEN0
  21. 9THEN
  22. 10 DBMS_OUTPUT.put_line (v_num |'Is even number');
  23. 11WHEN1
  24. 12THEN
  25. 13 DBMS_OUTPUT.put_line (v_num |Is odd number');
  26. 14ELSE
  27. 15NULL;
  28. 16END CASE;
  29. 17END;
  30. 18/
  31. Enter valueForIn_num: 5
  32. 5IsOdd number
  33. PL/SQLProcedureSuccessfully completed.
Ii. Search-type case statement
  1. --> Syntax
  2. CASE
  3. WHENSearch condition 1THENSTATEMENT 1;
  4. WHENSearch condition 2THENSTATEMENT 2;
  5. ...
  6. WHENSEARCH CONDITION NTHENStatement n;
  7. ELSEStatement n + 1;
  8. END CASE;
  9. -- 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,
  10. -- The result of the first true statement is returned. If the search condition after all when is not TRUE, the STATEMENT after else
  11. -- 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?
  12. --> The following example shows a search-type case.
  13. Scott @ ORCL>DECLARE
  14. 2 v_num NUMBER: = & in_num;
  15. 3BEGIN
  16. 4CASE
  17. 5WHENV_num> 0
  18. 6THEN
  19. 7 DBMS_OUTPUT.put_line (v_num |'Is a positive number');
  20. 8WHENV_num <0
  21. 9THEN
  22. 10 DBMS_OUTPUT.put_line (v_num |'Is a negative number');
  23. 11ELSE
  24. 12 DBMS_OUTPUT.put_line (v_num |Is zero');
  25. 13END CASE;
  26. 14END;
  27. 15/
  28. Enter valueForIn_num:-3
  29. -3IsA negative number
  30. --> 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.
  31. Scott @ ORCL>DECLARE
  32. 2 v_num NUMBER: = & in_num;
  33. 3BEGIN
  34. 4CASE
  35. 5WHENV_num> 0
  36. 6THEN
  37. 7 DBMS_OUTPUT.put_line (v_num |'Is a positive number');
  38. 8WHENV_num <0
  39. 9THEN
  40. 10 DBMS_OUTPUT.put_line (v_num |'Is a negative number');
  41. 11END CASE;
  42. 12END;
  43. 13/
  44. Enter valueForIn_num: 0
  45. DECLARE
  46. *
  47. ERRORAtLine 1:
  48. ORA-06592:CASE NotFound while executingCASEStatement
  49. ORA-06512:AtLine 4
  • 1
  • 2
  • Next Page

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.