Home » SQL & PL/SQL » SQL & PL/SQL » Using For Update in Cursor
Using For Update in Cursor [message #444081] Thu, 18 February 2010 11:29 Go to next message
navin_deep
Messages: 18
Registered: November 2009
Junior Member
Hi

I have come across a code where i need to know exact logic for the use of FOR UPDATE and COMMIT used in a cursor.

The code is like :

Declare
Cursor c1 is select * from emp FOR UPDATE;
Z c1%rowtype;
Begin
Open C1;
Fetch c1 into Z;
dbms_output.put_line(z.ename);
Commit;
Fetch c1 into Z;
dbms_output.put_line(z.ename);
end;

When i run above code, i get error as below, but first value from table emp i.e. emp.ename is displayed :

Error at line 1
ORA-01002: fetch out of sequence
ORA-06512: at line 9


But when i run by removing FOR UPDATE, i get first 2 values displayed without any error message:

Declare
Cursor c1 is select * from emp;
Z c1%rowtype;
Begin
Open C1;
Fetch c1 into Z;
dbms_output.put_line(z.ename);
Commit;
Fetch c1 into Z;
dbms_output.put_line(z.ename);
end;

Can anyone explain what is happening/logic here.

regards
-navin
Re: Using For Update in Cursor [message #444085 is a reply to message #444081] Thu, 18 February 2010 11:37 Go to previous messageGo to next message
BlackSwan
Messages: 25050
Registered: January 2009
Location: SoCal
Senior Member
01002, 00000, "fetch out of sequence"
// *Cause: This error means that a fetch has been attempted from a cursor
//         which is no longer valid.  Note that a PL/SQL cursor loop
//         implicitly does fetches, and thus may also cause this error.
//         There are a number of possible causes for this error, including:
//         1) Fetching from a cursor after the last row has been retrieved
//            and the ORA-1403 error returned.
//         2) If the cursor has been opened with the FOR UPDATE clause,
//            fetching after a COMMIT has been issued will return the error.
//         3) Rebinding any placeholders in the SQL statement, then issuing
//            a fetch before reexecuting the statement.
// *Action: 1) Do not issue a fetch statement after the last row has been
//             retrieved - there are no more rows to fetch.
//          2) Do not issue a COMMIT inside a fetch loop for a cursor
//             that has been opened FOR UPDATE.
//          3) Reexecute the statement after rebinding, then attempt to
//             fetch again.


It would be helpful if you followed Posting Guidelines - http://www.orafaq.com/forum/t/88153/0/
Re: Using For Update in Cursor [message #444095 is a reply to message #444081] Thu, 18 February 2010 12:05 Go to previous message
Michel Cadot
Messages: 64151
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Maybe we never told you but please read OraFAQ Forum Guide, especially "How to format your post?" section.
Make sure that lines of code do not exceed 80 characters when you format.
Indent the code, use code tags and align the columns in result.
Use the "Preview Message" button to verify.
Also always post your Oracle version with 4 decimals.

In the end, use SQL*Plus and copy and paste your session.

Regards
Michel
Previous Topic: ORA-01003: no statement parsed on CREATE TABLE
Next Topic: non schema user
Goto Forum:
  


Current Time: Fri Dec 09 13:51:07 CST 2016

Total time taken to generate the page: 0.13412 seconds