文档解释
ORA-01002: 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.
## ORA-01002: fetch out of sequence
ORA-01002: fetch out of sequence 是Oracle数据库中一个通用的错误代码,该错误表明当前操作被中断,因为在令一个非法的fetch操作中,应用程序试图从数据库中检索一行数据,而不是应该先执行的open操作。
官方解释
常见案例
这个错误的常见原因是在一个PL/SQL块中,用户在执行FETCH操作之前没有先执行OPEN操作,或者在CUrsor上执行了两次OPEN操作。 这会导致游标已经关闭,结果在执行FETCH之前,不能获取打开游标,从而导致ORA-10002: fetch out of sequence 错误消息出现。
正常处理方法及步骤
1.确定错误发生的PL/SQL块;
2.检查错误发生处,看是否在FETCH操作之前确实存在OPEN操作;
3.如果存在游标未关闭的情况,实施相应的函数,如 CLOSE CURSOR 等操作将之前的游标关闭;
4.重新编译PL/SQL语句块,重新执行。