Home >> FAQs/Tutorials >> Oracle Tutorials

Oracle Tutorials - Use FETCH Statement in a Loop

By: FYIcenter.com

(Continued from previous topic...)

How To Use FETCH Statement in a Loop?

If you have a cursor opened ready to use, you can also use the FETCH statement in a loop to retrieve data from the cursor more efficiently. But you need to remember to use an EXIT statement break the loop when the cursor pointer reaches the end. The script below gives you a good example:

CREATE OR REPLACE PROCEDURE FYI_CENTER AS
  CURSOR emp_cur IS SELECT * FROM employees
    WHERE manager_id = 101;
  emp_rec employees%ROWTYPE;
BEGIN
  OPEN emp_cur;
  LOOP
    FETCH emp_cur INTO emp_rec;
    EXIT WHEN emp_cur%NOTFOUND;
    DBMS_OUTPUT.PUT_LINE('Name = ' || 
      emp_rec.first_name || ' ' || emp_rec.last_name); 
  END LOOP; 
  CLOSE emp_cur;
END;
/
Name = Nancy Greenberg
Name = Jennifer Whalen
Name = Susan Mavris
Name = Hermann Baer
Name = Shelley Higgins

(Continued on next topic...)

  1. What Is a Cursor?
  2. How Many Types of Cursors Supported in PL/SQL?
  3. What Is the Implicit Cursor?
  4. How To Use Attributes of the Implicit Cursor?
  5. How To Loop through Data Rows in the Implicit Curosr?
  6. How To Define an Explicit Cursor?
  7. How To Open and Close an Explicit Cursor?
  8. How To Retrieve Data from an Explicit Cursor?
  9. How To Retrieve Data from a Cursor to a RECORD?
  10. How To Use FETCH Statement in a Loop?
  11. How To Use an Explicit Cursor without OPEN Statements?
  12. Can Multiple Cursors Being Opened at the Same Time?
  13. How To Pass a Parameter to a Cursor?
  14. What Is a Cursor Variable?
  15. How To Define a Cursor Variable?
  16. How To Open a Cursor Variable?
  17. How To Loop through a Cursor Variable?
  18. How To Pass a Cursor Variable to a Procedure?
  19. Why Cursor Variables Are Easier to Use than Cursors?

Oracle Tutorials:

More...


Other Tutorials/FAQs:

More...


Related Resources:

More...


Selected Jobs:

More...