PL-SQL
Oracle PL/SQL BULK COLLECT: FORALL Example
What is BULK COLLECT? BULK COLLECT reduces context switches between SQL and PL/SQL engine and...
A CASE statement is similar to IF-THEN-ELSIF statement that selects one alternative based on the condition from the available options.
Syntax:
CASE (expression) WHEN <valuel> THEN action_blockl; WHEN <value2> THEN action_block2; WHEN <value3> THEN action_block3; ELSE action_block_default; END CASE;
Example 1: Arithmetic Calculation using Case
In this example, we are going to do arithmetic calculation between two numbers 55 and 5.
DECLARE a NUMBER :=55; b NUMBER :=5; arth_operation VARCHAR2(20) :='MULTIPLY’; BEGIN dbms_output.put_line(‘Program started.' ); CASE (arth_operation) WHEN ‘ADD’ THEN dbms_output.put_line(‘Addition of the numbers are: '|| a+b ); WHEN ‘SUBTRACT' THEN dbms_output.put_line(‘Subtraction of the numbers are: '||a-b ); WHEN ‘MULTIPLY' THEN dbms_output.put_line(‘Multiplication of the numbers are: '|| a*b ); WHEN ‘DIVIDE' THEN dbms_output.put_line(‘Division of the numbers are:'|| a/b); ELSE dbms_output.put_line(‘No operation action defined. Invalid operation'); END CASE; dbms_output.put_line(‘Program completed.' ); END; /
Code Explanation:
Code Output:
Program started. Multiplication of the numbers are: 275 Program completed.
The SEARCHED CASE statement is similar to the CASE statement, rather than using the selector to select the alternative, SEARCHED CASE will directly have the expression defined in the WHEN clause.
Syntax:
CASE WHEN <expression1> THEN action_blockl; WHEN <expression2> THEN action_block2; WHEN <expression3> THEN action_block3; ELSE action_block_default; END CASE;
Example 1: Arithmetic Calculation using Searched Case
In this example, we are going to do arithmetic calculation between two numbers 55 and 5.
DECLARE a NUMBER :=55; b NUMBER :=5; arth_operation VARCHAR2(20) :='DIVIDE'; BEGIN dbms_output.put_line(‘Program started.' ); CASE WHEN arth_operation = 'ADD' THEN dbms_output.put_line(‘Addition of the numbers are: '||a+b ); WHEN arth_operation = ‘SUBTRACT' THEN dbms_output.put_line(‘Subtraction of the numbers are: '|| a-b); WHEN arth_operation = ‘MULTIPLY’ THEN dbms_output.put_line(‘Multiplication of the numbers are: '|| a*b ); WHEN arth_operation = ’DIVIDE' THEN dbms_output.put_line(‘Division of the numbers are: '|| a/b ): ELSE dbms_output.put_line(‘No operation action defined. Invalid operation'); END CASE; dbms_output.put_line(‘Program completed.' ); END; /
Code Explanation:
Code Output:
Program started. Division of the numbers are: 11 Program completed.
| TYPE | DESCRIPTION | USAGE |
|---|---|---|
CASE | Similar to IF-THEN-ELSIF statement. A 'SELECTOR' is used to choose the alternatives instead of Boolean expression. | Used to select from several alternatives using 'SELECTOR' |
SEARCHED CASE | CASE statement with no actual 'SELECTOR'. Instead, it contains the actual condition (which evaluates to TRUE/FALSE) that will select the alternatives. | Used to choose from more than two alternatives mostly. |
What is BULK COLLECT? BULK COLLECT reduces context switches between SQL and PL/SQL engine and...
What is For Loop? "FOR LOOP" statement is best suitable when you want to execute a code for a...
What is CURSOR in PL/SQL? A Cursor is a pointer to this context area. Oracle creates context area...
In this tutorial, we are going to learn how to use SQL in PL/SQL. SQL is the actual component that...
What is Dynamic SQL? Dynamic SQL is a programming methodology for generating and running...
What is Object Type in PL/SQL? Object-Oriented Programming is especially suited for building...