Edit D:\app\Administrator\product\11.2.0\dbhome_1\oc4j\j2ee\oc4j_applications\applications\em\em\online_help\tdddg\tdddg_subprograms028.htm
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <script src="./callback.js" type="text/javascript"></script> <noscript>Your browser does not support JavaScript. This help page requires JavaScript to render correctly.</noscript> </head> <meta http-equiv="Content-Type" content="text/html; charset=us-ascii" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <meta name="generator" content="Oracle DARB XHTML Converter (Mode = ohj/ohw) - Version 5.1.1 Build 005" /> <meta name="date" content="2009-04-21T9:46:24Z" /> <meta name="robots" content="noarchive" /> <meta name="doctitle" content="Using the CASE Statement" /> <meta name="relnum" content="11g Release 2 (11.2)" /> <meta name="partnum" content="E10766-01" /> <link rel="copyright" href="./dcommon/html/cpyr.htm" title="Copyright" type="text/html" /> <link rel="stylesheet" href="./dcommon/css/blafdoc.css" title="Oracle BLAFDoc" type="text/css" /> <link rel="contents" href="toc.htm" title="Contents" type="text/html" /> <link rel="prev" href="tdddg_subprograms027.htm" title="Previous" type="text/html" /> <link rel="next" href="tdddg_subprograms029.htm" title="Next" type="text/html" /> <title>Using the CASE Statement</title> </head> <body> <div class="zz-skip-header"><a href="#BEGIN">Skip Headers</a></div> <table class="simple oac_no_warn" summary="" cellspacing="0" cellpadding="0" width="100%"> <col width="86%" /> <col width="*" /> <tr valign="bottom"> <td align="left"></td> <td align="center"><a href="tdddg_subprograms027.htm"><img width="24" height="24" src="./dcommon/gifs/leftnav.gif" alt="Previous" /><br /> <span class="icon">Previous</span></a> </td> <td align="center"><a href="tdddg_subprograms029.htm"><img width="24" height="24" src="./dcommon/gifs/rightnav.gif" alt="Next" /><br /> <span class="icon">Next</span></a></td> </tr> </table> <p><a id="BABCGEIH" name="BABCGEIH"></a><a id="TDDDG44120" name="TDDDG44120"></a></p> <div class="sect2"><!-- infolevel="all" infotype="General" --> <h1>Using the CASE Statement</h1> <a name="BEGIN" id="BEGIN"></a> <p><a id="sthref427" name="sthref427"></a><a id="sthref428" name="sthref428"></a>The <code>CASE</code> statement chooses from a sequence of conditions, and executes the corresponding statement.</p> <p>The <a id="sthref429" name="sthref429"></a>simple <code>CASE</code> statement evaluates a single expression and compares it to several potential values. It has this syntax:</p> <pre xml:space="preserve" class="oac_no_warn"> CASE <span class="italic">expression</span> WHEN <span class="italic">value</span> THEN <span class="italic">statement</span> [ WHEN <span class="italic">value</span> THEN <span class="italic">statement</span> ]... [ ELSE <span class="italic">statement</span> [, <span class="italic">statement</span> ]... ] END CASE; </pre> <p>The <a id="sthref430" name="sthref430"></a>searched <code>CASE</code> statement evaluates multiple Boolean expressions and chooses the first one whose value is <code>TRUE</code>. For information about the searched <code>CASE</code> statement, see <a href="javascript:open('http://www.oracle.com/pls/db112/lookup?id=LNPLS99972','newWindow').focus()"><span class="italic">Oracle Database PL/SQL Language Reference</span></a>.</p> <div align="center"> <div class="inftblnotealso"><br /> <table class="NoteAlso oac_no_warn" summary="" cellpadding="3" cellspacing="0"> <tbody> <tr> <td align="left"> <p class="notep1">Tip:</p> When you can use either a <code>CASE</code> statement or nested <code>IF</code> statements, use a <code>CASE</code> statement—it is both more readable and more efficient.</td> </tr> </tbody> </table> <br /></div> <!-- class="inftblnotealso" --></div> <p>Suppose that, if an employee is evaluated only once a year, you want the <code>eval_frequency</code> function to suggest a salary increase, which depends on the <code>JOB_ID</code>.</p> <p>Add the <code>CASE</code> statement shown in the following example to the <code>eval_frequency</code> function. (For information about the procedure that prints the strings, <code>DBMS_OUTPUT.PUT_LINE</code>, see <a href="javascript:open('http://www.oracle.com/pls/db112/lookup?id=ARPLS036','newWindow').focus()"><span class="italic">Oracle Database PL/SQL Packages and Types Reference</span></a>.)</p> <div class="example"><a id="BABGJFBG" name="BABGJFBG"></a><a id="TDDDG230" name="TDDDG230"></a> <p class="titleinexample">CASE Statement that Determines Which String to Print</p> <pre xml:space="preserve" class="oac_no_warn"> FUNCTION eval_frequency (emp_id IN EMPLOYEES.EMPLOYEE_ID%TYPE) RETURN PLS_INTEGER AS h_date EMPLOYEES.HIRE_DATE%TYPE; today EMPLOYEES.HIRE_DATE%TYPE; eval_freq PLS_INTEGER; j_id EMPLOYEES.JOB_ID%TYPE; BEGIN SELECT SYSDATE INTO today FROM DUAL; SELECT HIRE_DATE INTO h_date FROM EMPLOYEES WHERE EMPLOYEE_ID = emp_id; IF ((h_date + (INTERVAL '120' MONTH)) < today) THEN eval_freq := 1; SELECT JOB_ID INTO j_id FROM EMPLOYEES WHERE EMPLOYEE_ID = emp_id; <span class="bold">CASE</span> j_id <span class="bold">WHEN</span> 'PU_CLERK' <span class="bold">THEN</span> DBMS_OUTPUT.PUT_LINE( 'Consider 8% salary increase for employee # ' || emp_id); <span class="bold">WHEN</span> 'SH_CLERK' <span class="bold">THEN</span> DBMS_OUTPUT.PUT_LINE( 'Consider 7% salary increase for employee # ' || emp_id); <span class="bold">WHEN</span> 'ST_CLERK' <span class="bold">THEN</span> DBMS_OUTPUT.PUT_LINE( 'Consider 6% salary increase for employee # ' || emp_id); <span class="bold">WHEN</span> 'HR_REP' <span class="bold">THEN</span> DBMS_OUTPUT.PUT_LINE( 'Consider 5% salary increase for employee # ' || emp_id); <span class="bold">WHEN</span> 'PR_REP' <span class="bold">THEN</span> DBMS_OUTPUT.PUT_LINE( 'Consider 5% salary increase for employee # ' || emp_id); <span class="bold">WHEN</span> 'MK_REP' <span class="bold">THEN</span> DBMS_OUTPUT.PUT_LINE( 'Consider 4% salary increase for employee # ' || emp_id); <span class="bold">ELSE</span> DBMS_OUTPUT.PUT_LINE( 'Nothing to do for employee #' || emp_id); <span class="bold">END CASE</span>; ELSE eval_freq := 2; END IF; RETURN eval_freq; END eval_frequency; </pre></div> <!-- class="example" --> <div class="helpinfonotealso"> <h2>Related Topics</h2> <p><a href="tdddg_selecting019.htm#BCGHIIGC">Using CASE Expressions in Queries</a></p> <p><a href="javascript:open('http://www.oracle.com/pls/db112/lookup?id=LNPLS01304','newWindow').focus()"><span class="italic">Oracle Database PL/SQL Language Reference</span></a> for the syntax of the <code>CASE</code> statement</p> <p><a href="javascript:open('http://www.oracle.com/pls/db112/lookup?id=LNPLS00402','newWindow').focus()"><span class="italic">Oracle Database PL/SQL Language Reference</span></a> for more information about using the <code>CASE</code> statement</p> <p><a href="tdddg_subprograms025.htm#CIHHFJFJ">Controlling Program Flow</a></p> </div> </div> <!-- class="sect2" --> <!-- Start Footer --> <div class="footer"> <table class="simple oac_no_warn" summary="" cellspacing="0" cellpadding="0" width="100%"> <col width="86%" /> <col width="*" /> <tr> <td align="left"><span class="copyrightlogo">Copyright © 1996, 2009, Oracle and/or its affiliates. All rights reserved.</span><br /> <a href="./dcommon/html/cpyr.htm"><span class="copyrightlogo">Legal Notices</span></a></td> <td align="center"><a href="tdddg_subprograms027.htm"><img width="24" height="24" src="./dcommon/gifs/leftnav.gif" alt="Previous" /><br /> <span class="icon">Previous</span></a> </td> <td align="center"><a href="tdddg_subprograms029.htm"><img width="24" height="24" src="./dcommon/gifs/rightnav.gif" alt="Next" /><br /> <span class="icon">Next</span></a></td> </tr> </table> </div> <!-- class="footer" --> </body> </html>
Ms-Dos/Windows
Unix
Write backup
jsp File Browser version 1.2 by
www.vonloesch.de