Oracle CASE expressions and CASE statements

Source: Internet
Author: User

The CASE expression is added to SQL in Oracle8i, and Oracle9i extends it so that the CASE expression and CASE statement can be used in PL/SQL.
 
CASE expression for value matching
The CASE expression is more flexible than the DECODE function. In its simplest application method, a specific value is returned when the matching is successful.
 
SELECT ename, empno,
(CASE deptno
WHEN 10 THEN 'accounting'
WHEN 20 THEN 'Research'
WHEN 30 THEN 'sales'
WHEN 40 THEN 'operation'
ELSE 'unknown'
END) department
FROM emp
Order by ename;
 
CASE expressions can also be used in PL/SQL.
SET SERVEROUTPUT ON
DECLARE
Deptno NUMBER: = 20;
Dept_desc VARCHAR2 (20 );
BEGIN
Dept_desc: = CASE deptno
WHEN 10 THEN 'accounting'
WHEN 20 THEN 'Research'
WHEN 30 THEN 'sales'
WHEN 40 THEN 'operation'
ELSE 'unknown'
END;
DBMS_OUTPUT.PUT_LINE (dept_desc );
END;
/
 
Search for a CASE expression
In a search CASE expression, a comparison expression is used as a matching condition. In this mode, the comparison condition is no longer limited to one column.
SELECT ename, empno,
(CASE
WHEN sal
WHEN sal BETWEEN 1000 AND 3000 THEN 'medium'
WHEN sal> 3000 THEN 'high'
ELSE 'n'/'
END) salary
FROM emp
Order by ename;
 
The search CASE expression can also be applied to PL/SQL.
SET SERVEROUTPUT ON
DECLARE
Sal NUMBER: = 2000;
Sal_desc VARCHAR2 (20 );
BEGIN
Sal_desc: = CASE
WHEN sal
WHEN sal BETWEEN 1000 AND 3000 THEN 'medium'
WHEN sal> 3000 THEN 'high'
ELSE 'n'/'
END;
DBMS_OUTPUT.PUT_LINE (sal_desc );
END;
/
 
CASE statement for value matching
PL/SQL also supports value matching CASE statements. The usage is very similar to the CASE expression. The main difference is that the CASE statement ends with END CASE rather than END with the CASE expression. The CASE statement can be used as a replacement for IF... THEN... ELSEIF.
SET SERVEROUTPUT ON
BEGIN
FOR cur_rec IN (SELECT ename, empno, deptno FROM emp order by ename) LOOP
DBMS_OUTPUT.PUT (cur_rec.ename | ':' | cur_rec.empno | ':');
CASE cur_rec.deptno
WHEN 10 THEN
DBMS_OUTPUT.PUT_LINE ('accounting ');
WHEN 20 THEN
DBMS_OUTPUT.PUT_LINE ('Research ');
WHEN 30 THEN
DBMS_OUTPUT.PUT_LINE ('sales ');
WHEN 40 THEN
DBMS_OUTPUT.PUT_LINE ('operations ');
ELSE
DBMS_OUTPUT.PUT_LINE ('unknown ');
End case;
End loop;
END;
/
 
Search CASE statements
Like a CASE expression, this statement can be used to compare multiple variables.
SET SERVEROUTPUT ON
BEGIN
FOR cur_rec IN (SELECT ename, empno, sal FROM emp order by ename) LOOP
DBMS_OUTPUT.PUT (cur_rec.ename | ':' | cur_rec.empno | ':');
CASE
WHEN cur_rec.sal
DBMS_OUTPUT.PUT_LINE ('low ');
WHEN cur_rec.sal BETWEEN 1000 AND 3000 THEN
DBMS_OUTPUT.PUT_LINE ('medium ');
WHEN cur_rec.sal> 3000 THEN
DBMS_OUTPUT.PUT_LINE ('high ');
ELSE
DBMS_OUTPUT.PUT_LINE ('unknown ');
End case;
End loop;
END;
/

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.