PLSQL Cursors

9,274 views

Published on

Published in: Technology, Business

PLSQL Cursors

  1. 1. PL/SQL Cursors By Praveen & Naresh
  2. 2. What is Cursors ? <ul><li>SQL: </li></ul><ul><li>> SELECT * FROM employee; </li></ul><ul><li>Oracle RDBMS : Assigns a private work area for that stmt. </li></ul>
  3. 3. contd.. <ul><li>PL/SQL cursor is a mechanism by which </li></ul><ul><li>“ you can NAME that work area </li></ul><ul><li>and </li></ul><ul><li>manipulate the information within it” </li></ul>
  4. 4. Declare/Open/Fetch/Close <ul><li>CURSOR emp_cur IS SELECT * FROM employee ; </li></ul><ul><li>Open emp_cur ; </li></ul><ul><li>Fetch emp_cur INTO emp_rec; </li></ul><ul><li>CLOSE emp_cur ; </li></ul>
  5. 5. Types of cursors <ul><li>Two Types: </li></ul><ul><li>1) Implicit </li></ul><ul><li>2) Explicit </li></ul>
  6. 6. Cursor Attributes <ul><li>Attribute: </li></ul><ul><li>%FOUND </li></ul><ul><li>%ISOPEN </li></ul><ul><li>%NOTFOUND </li></ul><ul><li>%ROWCOUNT </li></ul>
  7. 7. Implicit Cursors <ul><li>DML Statements </li></ul><ul><li>INSERT, UPDATE, DELETE </li></ul><ul><li>SELECT statement with INTO clause </li></ul>
  8. 8. Single Row Implicit Cursors <ul><li>DECLARE </li></ul><ul><li>e_id employee.empid%TYPE; </li></ul><ul><li>e_name employee.empname%TYPE; </li></ul><ul><li>BEGIN </li></ul><ul><li>SELECT empid, empname INTO e_id, e_name </li></ul><ul><li>FROM employee where empid=23; </li></ul><ul><li>dbms_output.put_line( SQL%ROWCOUNT ); </li></ul><ul><li>END; </li></ul><ul><li>/ </li></ul>
  9. 9. Multiple Row Implicit Cursors(1) <ul><li>BEGIN </li></ul><ul><li>UPDATE employee SET emp_sal= emp_sal + 5000 </li></ul><ul><li>where emp_deptid=1; </li></ul><ul><li>IF SQL%FOUND THEN </li></ul><ul><li>dbms_output.put_line( ' Updated: '|| SQL%ROWCOUNT ); </li></ul><ul><li>ELSE </li></ul><ul><li>dbms_output.put_line( 'Nothing Updated' ); </li></ul><ul><li>END; </li></ul><ul><li>/ </li></ul>
  10. 10. Multiple Row Implicit Cursors(2) <ul><li>BEGIN </li></ul><ul><li>FOR e_cursor IN ( SELECT e_name FROM employee ) LOOP </li></ul><ul><li>dbms_output.put_line( e_cursor.e_name ); </li></ul><ul><li>END LOOP; </li></ul><ul><li>END; </li></ul><ul><li>/ </li></ul>
  11. 11. Cursor FOR Loop <ul><li>Scope : Inside FOR Loop </li></ul><ul><li>Cursor Index : </li></ul><ul><li>- a pointer to Query work area. </li></ul><ul><li>- Query work area is a memory region ( context area ) </li></ul><ul><li>Note: </li></ul><ul><li>SQL%ROWCOUNT attribute returns NULL . </li></ul>
  12. 12. Explicit Cursors <ul><li>Define inside declaration block </li></ul><ul><li>Static or dynamic SELECT stmt. </li></ul><ul><li>Open/Fetch/Close </li></ul>
  13. 13. Syntax <ul><li>OPEN cursor_name [ (param1 , param2 ,....) ] </li></ul><ul><li>FETCH cursor_name INTO (variable1,variable2,....)‏ </li></ul><ul><li>FETCH cursor_name INTO record_variable; </li></ul><ul><li>CLOSE cursor_name ; </li></ul>
  14. 14. Static Explicit Cursors <ul><li>SQL SELECT – behavior doesn't change </li></ul>
  15. 15. Static Cursor <ul><li>DECLARE </li></ul><ul><li>eid employee.empid%TYPE; </li></ul><ul><li>ename employee.empname%TYPE; </li></ul><ul><li>CURSOR cur IS </li></ul><ul><li>SELECT empid, empname FROM employee ; </li></ul><ul><li>BEGIN </li></ul><ul><li>OPEN cur ; </li></ul><ul><li>LOOP </li></ul><ul><li>FETCH cur INTO eid , ename ; </li></ul><ul><li>EXIT WHEN cur%NOTFOUND ; </li></ul><ul><li>dbms_output.put_line( 'NAME' || cur . ename ); </li></ul><ul><li>END LOOP; </li></ul><ul><li>CLOSE cur ; </li></ul><ul><li>END; </li></ul><ul><li>/ </li></ul>
  16. 16. Eg- Cursor For Loop <ul><li>DECLARE </li></ul><ul><li>CURSOR C IS </li></ul><ul><li>SELECT empid AS eid , empname AS ename FROM employee ; </li></ul><ul><li>BEGIN </li></ul><ul><li>FOR i IN C LOOP </li></ul><ul><li>dbms_output.put_line( 'NAME' || i.ename ); </li></ul><ul><li>END LOOP ; </li></ul><ul><li>END; </li></ul><ul><li>/ </li></ul>
  17. 17. Static Cursor( Record)‏ <ul><li>DECLARE </li></ul><ul><li>TYPE emp_record IS RECORD ( eid NUMBER, ename VARCHAR2(30) ); </li></ul><ul><li>employee EMP_RECORD ; </li></ul><ul><li>CURSOR cur IS SELECT empid, empname FROM employee ; </li></ul><ul><li>BEGIN </li></ul><ul><li>OPEN cur ; </li></ul><ul><li>LOOP </li></ul><ul><li>FETCH cur INTO employee ; </li></ul><ul><li>EXIT WHEN cur%NOTFOUND ; </li></ul><ul><li>dbms_output.put_line( 'NAME' || employee . ename ); </li></ul><ul><li>END LOOP; </li></ul><ul><li>CLOSE cur ; </li></ul><ul><li>END; </li></ul><ul><li>/ </li></ul>
  18. 18. Static Cursor( Record+FOR Loop)‏ <ul><li>DECLARE </li></ul><ul><li>TYPE emp_record IS RECORD ( eid NUMBER, ename VARCHAR2(30) ); </li></ul><ul><li>explicit_employee EMP_RECORD ; </li></ul><ul><li>CURSOR cur IS SELECT empid, empname FROM employee ; </li></ul><ul><li>BEGIN </li></ul><ul><li>FOR i IN cur LOOP </li></ul><ul><li>explicit_employee := i; </li></ul><ul><li>dbms_output.put_line( 'NAME' || explicit_employee . ename ); </li></ul><ul><li>END LOOP; </li></ul><ul><li>END; </li></ul><ul><li>/ </li></ul><ul><li>Note: No Data Found , No error is raised </li></ul>
  19. 19. Static Cursor( No rows)‏ <ul><li>DECLARE </li></ul><ul><li>TYPE emp_record IS RECORD ( eid NUMBER, ename VARCHAR2(30) ); </li></ul><ul><li>employee EMP_RECORD ; </li></ul><ul><li>CURSOR cur IS SELECT empid, empname FROM employee where empid = -1 ; </li></ul><ul><li>BEGIN </li></ul><ul><li>OPEN cur ; </li></ul><ul><li>LOOP </li></ul><ul><li>FETCH cur INTO employee ; </li></ul><ul><li>IF cur%NOTFOUND THEN </li></ul><ul><li>IF cur%ROWCOUNT = 0 THEN </li></ul><ul><li>dbms_output.put_line( 'No Data Found' ); </li></ul><ul><li>END IF; </li></ul><ul><li>EXIT; </li></ul><ul><li>ELSE </li></ul><ul><li>dbms_output.put_line( 'NAME' || employee . ename ); </li></ul><ul><li>END IF; </li></ul><ul><li>END LOOP; </li></ul><ul><li>CLOSE cur ; </li></ul><ul><li>END; </li></ul><ul><li>/ </li></ul>
  20. 20. Thanks

×