A Language is not worth knowing unless it teaches you to think differently.
Tuesday, December 25, 2012
Cursor FOR Loops
The cursor FOR loop provides an elegant, simple syntax to to iterate over a result set. To underscore the advantages of cursor FOR loops, consider the following PL/SQL block which uses a basic loop.
SET SERVEROUTPUT ON
DECLARE
-- EMP_CURSOR will retrieve all columns and all rows from the EMP table
CURSOR emp_cursor IS
SELECT *
FROM emp;
emp_record emp_cursor%ROWTYPE;
BEGIN
OPEN emp_cursor;
LOOP
--Advance the pointer in the result set, assign row values to EMP_RECORD
FETCH emp_cursor INTO emp_record;
--Test to see if no more results
EXIT WHEN emp_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(emp_record.ename||' [' ||emp_record.empno||']');
END LOOP;
CLOSE emp_cursor;
END;
/
Now examine the same query using a cursor FOR loop:
DECLARE
-- EMP_CURSOR will retrieve all columns and all rows from the EMP table
CURSOR emp_cursor IS
SELECT *
FROM emp;
BEGIN
FOR emp_record IN emp_cursor LOOP
DBMS_OUTPUT.PUT_LINE(emp_record.ename||' ['||emp_record.empno||']');
END LOOP;
END;
/
You can use a cursor for loop without a declared cursor by including a query in the FOR statement. This can enable very compact code.
BEGIN
FOR emp_record IN (SELECT * FROM emp) LOOP
DBMS_OUTPUT.PUT_LINE(emp_record.ename||' ['||emp_record.empno||']');
END LOOP;
END;
/
While you can use EXIT statement within a FOR cursor loop, you should not use a cursor FOR loop if you may need to exit the LOOP prematurely. Use a basic or WHILE loop instead.
Subscribe to:
Post Comments (Atom)
Price List Query for Item
SELECT qph.list_header_id, qph.name, qph.description, qphh.start_date_active, qphh.currency_code, q...
-
1. To download Ldt file for a Concurent Program FNDLOAD [username]/[password] O Y DOWNLOAD $FND_TOP/patch/115/import/afcpprog.lct file_nam...
-
Here the article will describe about how to register WebADI to Responsibility menu Go to System Administrator --> Security --> Respo...
-
using the below query can check the invoice status. SELECT DECODE(AP_INVOICES_UTILITY_PKG.GET_APPROVAL_STATUS(AIA.INVOICE_...
No comments:
Post a Comment