cursor and exception handling by nidhi bhatnagar

Download Cursor and Exception Handling By Nidhi Bhatnagar

Post on 27-Dec-2015




0 download

Embed Size (px)


  • Slide 1
  • Cursor and Exception Handling By Nidhi Bhatnagar
  • Slide 2
  • Objectives Cursor with For Loops Cursor with Parameters For Update of - Where Current of Cursor Ref Cursor Exception Handling Understanding Exception Handling various types
  • Slide 3
  • Cursor with For Loops It makes the code bit easier to maintain and use if we make use of FOR Loops in Cursors. Reason, it reduces the number of steps needed while working with cursor. Cursor in case of FOR Loops do not require Open, Fetch or Close cursor steps.
  • Slide 4
  • Cursor with For Loops It becomes a two step process Step1 : Declare a cursor Step2 : Declare a Record Type or Row Type variable and just use it in For Loop with Cursor
  • Slide 5
  • Cursor with For Loops Example Declare Cursor c_emp IS Select * From Emp; v_emp c_emp%Rowtype; Begin For v_emp in c_emp Loop dbms_output.put_line(v_emp.empno || || v_emp.sal); End Loop; End;
  • Slide 6
  • Cursor with For Loops For Loop automatically: Open a cursor Fetch from first to last record in a cursor Query. Close the cursor. It is thus simple to work with For Loops in case of cursors.
  • Slide 7
  • Cursor with For Loops However there is slight change in For Loop when we work with cursor. Normally it is : For i in 1..10 In case of cursor it is: For variable IN cursor For Variable IN Cursor Loop End Loop;
  • Slide 8
  • Example FOR record_index in cursor_name LOOP {.statements.} END LOOP;
  • Slide 9
  • Cursor with Parameters PL/SQL also allows you to pass parameters into cursors. The rationale for using parameters in cursors is that it makes the cursor more reusable. Instead of hardcoding a value into the WHERE clause of a query to select particular information, you can use a parameter and then pass different values to the WHERE clause each time a cursor is opened.
  • Slide 10
  • Cursor with Parameters In case of cursor with parameters step1 and step2 Declare Cursor Open Cursor changes: Step3 and Step4 Fetch cursor Close cursor remains the same
  • Slide 11
  • Cursor with Parameters Changes in Declare Cursor Cur_name(parameter1, parameter2