12.07.2015 Views

Oracle SQL Developer

Oracle SQL Developer

Oracle SQL Developer

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Utilizing the Main Features of PL/<strong>SQL</strong>END;/Example 7–13 shows the use of IF-THEN-ELSEIF-ELSE to determine the salary raisean employee receives based on the hire date of the employee.Example 7–13Using the IF-THEN-ELSEIF StatementDECLAREbonus NUMBER(6,2);emp_id NUMBER(6) := 120;hire_date DATE;BEGINSELECT hire_date INTO hire_date FROM employees WHERE employee_id = 120;IF hire_date > TO_DATE('01-JAN-98') THENbonus := 500;ELSIF hire_date > TO_DATE('01-JAN-96') THENbonus := 1000;ELSEbonus := 1500;END IF;UPDATE employees SET salary = salary + bonus WHERE employee_id = emp_id;END;/7.3.11.2 Conditional Control With the CASE StatementTo choose among several values or courses of action, you can use CASE constructs. TheCASE expression evaluates a condition and returns a value for each case. The casestatement evaluates a condition and performs an action, such as an entire PL/<strong>SQL</strong>block, for each case. When possible, rewrite lengthy IF-THEN-ELSIF statements asCASE statements because the CASE statement is more readable and more efficient.Example 7–14 shows a simple CASE statement.Example 7–14Using the CASE-WHEN StatementDECLAREgrade CHAR(1);BEGINgrade := 'B';CASE gradeWHEN 'A' THEN DBMS_OUTPUT.PUT_LINE('Excellent');WHEN 'B' THEN DBMS_OUTPUT.PUT_LINE('Very Good');WHEN 'C' THEN DBMS_OUTPUT.PUT_LINE('Good');WHEN 'D' THEN DBMS_OUTPUT.PUT_LINE('Fair');WHEN 'F' THEN DBMS_OUTPUT.PUT_LINE('Poor');ELSE DBMS_OUTPUT.PUT_LINE('No such grade');END CASE;END;/Example 7–15 determines the salary raise an employee receives based on the currentsalary of the employee and the job Id. This complex example combines the CASEexpression with IF-THEN-ELSE statements.Example 7–15 Using the IF-THEN_ELSE and CASE StatementDECLARE -- declare variablesjobid employees.job_id%TYPE;7-12 <strong>SQL</strong> <strong>Developer</strong> Online Help

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!