Oracle case statement syntax

Source: Internet
Author: User

The Oracle case statement is one of the most commonly used statements. The following describes the syntax of the Oracle case statement and provides an example to help you.

When multiple condition branch statements are executed, the Oracle case statement is more concise and more efficient. There are two methods for Oracle case statements to process multi-condition branch statements. The first method is to use a single selector for equivalent comparison. The second method is to use multiple conditions for non-equivalent comparison.

1. Use a single selector for equivalent comparison
When you execute an Oracle case statement to execute multiple condition branches, if the condition selection operator is identical and the condition expression is the same condition selection, you can select a single selection operator for equivalent comparison. The syntax is as follows:

Case condition Selector
When condition value expression 1 then the operation to be executed 1;
When condition value expression 2 then the operation to be executed 2;
.......
Else
The operation that you want to perform.
End case;

Example:

 
 
  1. Declare
  2. V_count number;
  3. Begi
  4. Select count (*) into v_count from cip_temps;
  5. Case v_count
  6. When 1 then
  7. Dbms_output.put_line ('when 1 operation ___ v_cont value: '| v_count );
  8. When 5 then
  9. Dbms_output.put_line ('when 5 Operation ___ v_count value: '| v_count );
  10. When 10 then
  11. Dbms_output.put_line ('when 10 Operator ____ v_count value: '| v_count );
  12. Else
  13. Dbms_output.put_line ('else operator ____ v_cout value: '| v_count );
  14. End case;
  15. End;
  16. /

2. Compare multiple conditions in case
If you select multiple conditions for different comparisons, you must specify the comparison conditions in the when clause. The syntax is as follows:

Case
When condition value expression 1 then the operation to be executed 1;
When condition value expression 2 then the operation to be executed 2;
.......
Else
The operation that you want to perform.
End case;

Example:

 
 
  1. Declare
  2. V_count number;
  3. Begin
  4. Select count (*) into v_count from cip_temps;
  5. Case
  6. When v_count> 10 then
  7. Dbms_output.put_line ('when 1 operation ___ v_cont value: '| v_count );
  8. When v_count> 5 then
  9. Dbms_output.put_line ('when 5 Operation ___ v_count value: '| v_count );
  10. When v_count> 4 then
  11. Dbms_output.put_line ('when 10 Operator ____ v_count value: '| v_count );
  12. Else
  13. Dbms_output.put_line ('else operator ____ v_cout value: '| v_count );
  14. End case;
  15. End;

Oracle sqlplus statement Editing Command

Oracle user syntax Modification

Oracle Default User Password Problems

Show you the Oracle explicit cursor

Use instances of Oracle stored procedures

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.