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>sequence of statements is executed, then control resumes at the top of the loop. If thecondition is false or null, the loop is bypassed and control passes to the next statement.In Example 7–17, you find the first employee who has a salary over $15000 and ishigher in the chain of command than employee 120:Example 7–17 Using WHILE-LOOP for Control-- create a temporary table for this exampleCREATE TABLE temp (tempid NUMBER(6), tempsal NUMBER(8,2), tempname VARCHAR2(25));DECLARE -- declare variablessal employees.salary%TYPE := 0;mgr_idemployees.manager_id%TYPE;lnameemployees.last_name%TYPE;starting_empid employees.employee_id%TYPE := 120;BEGINSELECT manager_id INTO mgr_id FROM employeesWHERE employee_id = starting_empid; -- retrieve data from employees-- use WHILE LOOP to process dataWHILE sal 15000SELECT salary, manager_id, last_name INTO sal, mgr_id, lnameFROM employees WHERE employee_id = mgr_id;END LOOP;INSERT INTO temp VALUES (NULL, sal, lname); -- insert NULL for tempid in tableCOMMIT;EXCEPTIONWHEN NO_DATA_FOUND THENINSERT INTO temp VALUES (NULL, NULL, 'Not found'); -- insert NULLsCOMMIT;END;/-- display rows in table tempSELECT * FROM temp;-- drop temporary tableDROP TABLE temp;The EXIT-WHEN statement lets you complete a loop if further processing is impossibleor undesirable. When the EXIT statement is encountered, the condition in the WHENclause is evaluated. If the condition is true, the loop completes and control passes tothe next statement. In Example 7–18, the loop completes when the value of totalexceeds 25,000:Example 7–18 Using the EXIT-WHEN StatementDECLARE -- declare and assign values to variablestotal NUMBER(9) := 0;counter NUMBER(6) := 0;BEGINLOOPcounter := counter + 1; -- increment counter variabletotal := total + counter * counter; -- compute total-- exit loop when condition is trueEXIT WHEN total > 25000; -- LOOP until condition is metEND LOOP;DBMS_OUTPUT.PUT_LINE('Counter: ' || TO_CHAR(counter) || ' Total: ' || TO_CHAR(total)); -- display dataEND;/7-14 <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!