12.07.2015 Views

Oracle SQL Developer

Oracle SQL Developer

Oracle SQL Developer

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Utilizing the Main Features of PL/<strong>SQL</strong>empid employees.employee_id%TYPE := 115;sal employees.salary%TYPE;sal_raise NUMBER(3,2);BEGIN-- retrieve data from employees and assign to variables jobid and salSELECT job_id, salary INTO jobid, sal from employees WHERE employee_id = empid;CASE -- check for conditionsWHEN jobid = 'PU_CLERK' THENIF sal < 3000 THEN sal_raise := .08;ELSE sal_raise := .07;END IF;WHEN jobid = 'SH_CLERK' THENIF sal < 4000 THEN sal_raise := .06;ELSE sal_raise := .05;END IF;WHEN jobid = 'ST_CLERK' THENIF sal < 3500 THEN sal_raise := .04;ELSE sal_raise := .03;END IF;ELSEBEGIN-- if no conditions met, then the followingDBMS_OUTPUT.PUT_LINE('No raise for this job: ' || jobid);END;END CASE;UPDATE employees SET salary = salary + salary * sal_raiseWHERE employee_id = empid; -- update a record in the employees tableCOMMIT;END;/A sequence of statements that uses query results to select alternative actions iscommon in database applications. Another common sequence inserts or deletes a rowonly if an associated entry is found in another table. You can bundle these commonsequences into a PL/<strong>SQL</strong> block using conditional logic.7.3.11.3 Iterative Control With LOOPsLOOP statements let you execute a sequence of statements multiple times. You placethe keyword LOOP before the first statement in the sequence and the keywords ENDLOOP after the last statement in the sequence.The FOR-LOOP statement lets you specify a range of integers, then execute a sequenceof statements once for each integer in the range. In Example 7–16 the loop displays thenumber and the square of the number for numbers 1 to 10. inserts 100 numbers, squareroots, squares, and the sum of squares into a database table:Example 7–16 Using the FOR-LOOPBEGIN-- use a FOR loop to process a series of numbersFOR i in 1..10 LOOPDBMS_OUTPUT.PUT_LINE('Number: ' || TO_CHAR(i) || ' Square: ' || TO_CHAR(i*i));END LOOP;END;/The WHILE-LOOP statement associates a condition with a sequence of statements.Before each iteration of the loop, the condition is evaluated. If the condition is true, thePL/<strong>SQL</strong>: Usage Information 7-13

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

Saved successfully!

Ooh no, something went wrong!