Basic cursors in oracle

510 views

Published on

Basic Cursors in PL/SQL-Oracle

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
510
On SlideShare
0
From Embeds
0
Number of Embeds
139
Actions
Shares
0
Downloads
15
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Basic cursors in oracle

  1. 1. Cursors In Oracle Suhel Firdus
  2. 2. Definition • A cursor is a temporary work area created in the system memory when a SQL statement is executed. A cursor contains information on a select statement and the rows of data accessed by it. • Can hold information of more than one row. • One row at a time can be accessed.
  3. 3. Cursor types • Implicit Cursor • Explicit Cursor
  4. 4. Implicit Cursor • Oracle internally creates Implicit cursor • Whenever a DML operation like SELECT,UPDATE,INSERT happens on a row Oracle creates the cursor internally. • SELECT on a single row is a implicit cursor. • Status of the Implicit cursor can be found using SQL%. • Use cannot specify a name to Implicit Cursor.
  5. 5. Implicit Cursor SQL%FOUND The return value is TRUE, if the DML statements like INSERT, DELETE and UPDATE affect at least one row and if SELECT ….INTO statement return at least one row. SQL%ROWCOUNT The return value is FALSE, if DML statements like INSERT, DELETE and UPDATE at least one row and if SELECT ….INTO statement return at least one row SQL%NOTFOUND Return the number of rows affected by the DML operations INSERT, DELETE, UPDATE, SELECT
  6. 6. Explicit Cursor • User defines the Explicit Cursor • Involves specific operations to use the cursor. • Has a definite name. • Multiple rows are selected using Explicit Cursor
  7. 7. Defining Explicit Cursor Declare CURSOR cur_xmp_cursor IS (SELECT * from emplyees_tab); Begin ----<other code> Exception ---<exception handling> END: Cursor Name Table Name
  8. 8. Cursor Operations • Define the cursor. • Open the Cursor. • Fetch the cursor • Close the cursor.
  9. 9. Operations Explained. DECLARE CURSOR curr_exp_examp IS (SELECT * FROM employees_tab); L_employee_row employees_tab%ROWTYPE; BEGIN OPEN curr_exp_examp ; LOOP FETCH curr_exp_examp INTO L_employee_row ; < CODE TO PROCESS> EXIT WHEN curr_exp_examp %NOTFOUND; END LOOP; CLOSE curr_exp_examp ; END;
  10. 10. Example • DECLARE • CURSOR CUR_EXP_EXAMP IS( • SELECT * • FROM EMPLOYEES); • L_EMP_ROW EMPLOYEES%ROWTYPE; ---- OR L_EMP_ROW CUR_EXP_EXAMP%ROWTYPE; • BEGIN • OPEN CUR_EXP_EXAMP; • LOOP • FETCH CUR_EXP_EXAMP • INTO L_EMP_ROW; • DBMS_OUTPUT.PUT_LINE('employee name->' || L_EMP_ROW.EMP_NAME); • EXIT WHEN CUR_EXP_EXAMP%NOTFOUND; • END LOOP; • CLOSE CUR_EXP_EXAMP; • END;

×