Beruflich Dokumente
Kultur Dokumente
EMAIL
VIEW RANDOM POST
News, views, tips and tricks on Oracle, ColdFusion and other fun stuff
« GET ALL THE COOL NEW FEATURES OF ORACLE APPLICATION EXPRESS 3.0 IN ORACLE DATABASE XE
Let’s first do a quick review of CASE in PL/SQL. In general, you have two types, CASE statements and CASE expressions.
further categorize each as simple or searched:
CASE expression
WHEN result THEN
statement;
statement;
...
WHEN result THEN
statement;
statement;
...
...
ELSE
statement;
statement;
...
END CASE;
For example:
SQL> DECLARE
2 grade CHAR (1);
3 BEGIN
4 grade := 'A';
5
6 CASE grade
7 WHEN 'A'
8 THEN
9 DBMS_OUTPUT.put_line ('Excellent');
10 DBMS_OUTPUT.put_line ('You are smart');
11 WHEN 'B'
12 THEN
13 DBMS_OUTPUT.put_line ('Very Good');
14 WHEN 'C'
15 THEN
16 DBMS_OUTPUT.put_line ('Good');
17 ELSE
18 DBMS_OUTPUT.put_line ('Go home');
19 END CASE;
20 END;
21 /
Excellent
You are smart
CASE
WHEN boolean_expression THEN
statement;
statement;
...
WHEN boolean_expression THEN
statement;
statement;
...
...
ELSE
statement;
statement;
...
END CASE;
For example:
SQL> DECLARE
2 grade CHAR (1);
3 BEGIN
4 grade := 'X';
5
6 CASE
7 WHEN grade = 'A'
8 THEN
9 DBMS_OUTPUT.put_line ('Excellent');
10 WHEN grade = 'B'
11 THEN
12 DBMS_OUTPUT.put_line ('Very Good');
13 END CASE;
14 EXCEPTION
15 WHEN case_not_found
16 THEN
17 DBMS_OUTPUT.put_line ('No such grade');
18 END;
19 /
No such grade
1. The CASE statement executes one or many statements based on a certain condition.
2. If no ELSE is specified, and no expressions evaluate to TRUE, then the exception CASE_NOT_FOUND is raised.
3. WHEN clauses are evaluated in order, from top to bottom.
case_result :=
CASE expression
WHEN result THEN
result_expression;
WHEN result THEN
result_expression;
...
ELSE
result_expression;
END;
For example:
SQL> DECLARE
2 grade CHAR (1) := 'B';
3 appraisal VARCHAR2 (20);
4 BEGIN
5 appraisal :=
6 CASE grade
7 WHEN 'A'
8 THEN 'Excellent'
9 WHEN 'B'
10 THEN 'Very Good'
11 ELSE 'No such grade'
12 END;
13 DBMS_OUTPUT.put_line ('Grade ' || grade || ' is ' || appraisal);
14 END;
15 /
Grade B is Very Good
case_result :=
CASE
WHEN boolean_expression THEN
result_expression;
WHEN boolean_expression THEN
result_expression;
...
ELSE
result_expression;
END;
For example:
SQL> DECLARE
2 grade CHAR (1) := 'Z';
3 appraisal VARCHAR2 (20);
4 BEGIN
5 appraisal :=
6 CASE
7 WHEN grade = 'A'
8 THEN 'Excellent'
9 WHEN grade = 'B'
10 THEN 'Very Good'
11 END;
12 DBMS_OUTPUT.put_line
13 ('Grade ' || grade || ' is ' || NVL(appraisal, 'not graded'));
14 END;
15 /
Grade Z is Not graded
Function created.
When you execute the above function, passing a value that is not ‘A’ and not ‘B’, the CASE expression should return NULL
SQL> BEGIN
2 DBMS_OUTPUT.put_line ( 'Expression good for you = '
3 || NVL (fruit_translator ('C'), 'Unknown Fruit')
4 );
5 END;
6 /
Expression good for you = Unknown Fruit
As expected.
Function created.
When you execute the above function passing a value that is not ‘A’ and not ‘B’, and because there is no ELSE specified, t
statement should raise an (unhandled) exception:
SQL> BEGIN
2 DBMS_OUTPUT.put_line ( 'Statement good for you = '
3 || NVL (fruit_translator ('C'), 'Unknown Fruit')
4 );
5 END;
6 /
BEGIN
*
ERROR at line 1:
ORA-06592: CASE not found while executing CASE statement
ORA-06512: at "EDDIE.FRUIT_TRANSLATOR", line 7
ORA-06512: at line 2
As expected.
One more thing to note about CASE. In Oracle 8i, CASE statements and expressions are supported only in SQL and not in
In 8i you cannot use CASE in PL/SQL. Starting with Oracle database release 9.0.1, the SQL and PL/SQL parsers were integ
and, as a result, in version 9.0.1 and above CASE works in both SQL and PL/SQL.
Thank you Steven for this nice puzzler. It’s easy to solve (without executing the code ) as long as you know the differe
between CASE statements and CASE expressions.
Share This
Email:
Find
Spam protection: Sum of four + seven ?
Recent Posts *
8 Links from Around the Web (2007-07-13) c Notify me of followup comments via e-mail
d
e
f
g
Oracle Blogs Ranked by Technorati Authority - July 07
Edition
Related Posts