CASE in SQL Statements
Support for CASE in SQL statements preceded support for CASE in PL/SQL; Oracle8i Database introduced the searched CASE expression in SQL. Unfortunately, much to the frustration of PL/SQL programmers everywhere, SQL statements executed from within PL/SQL could not use CASE expressions. For example, from SQL*Plus you could execute:
SELECT
CASE
WHEN DUMMY='X' THEN 'Dual is OK'
ELSE 'Dual is messed up'
END
FROM DUAL;
However, the following PL/SQL code would not work:
DECLARE
dual_message VARCHAR2(20);
BEGIN
SELECT CASE
WHEN DUMMY='X' THEN 'Dual is OK'
ELSE 'Dual is messed up'
END INTO dual_message
FROM DUAL;
DBMS_OUTPUT.PUT_LINE (dual_message);
END;
This frustration goes away starting with Oracle9i Database Release 1. From this release onwards, you can have both searched and simple CASE expressions, and they can be used equally from SQL, from PL/SQL, and from SQL within PL/SQL.
Partager