ORA-01002: fetch out of sequence
Cause: In a host language program, a FETCH call was issued out of sequence. A successful parse-and-execute call must be issued before a fetch. This can occur if an attempt was made to FETCH from an active set after all records have been fetched. This may be caused by fetching from a SELECT FOR UPDATE cursor after a commit. A PL/SQL cursor loop implicitly does fetches and may also cause this error.
Action: Parse and execute a SQL statement before attempting to fetch the data.
实际应用及解决方法:
1、在你取完部分数据并执行的过程中,可能有commit或者rollback语句,导致在表t上加的lock被释放掉,再取数据的时候导致出错。
Fetching Across Commits
The FOR UPDATE clause acquires exclusive row locks. All rows are locked when you
open the cursor, and they are unlocked when you commit your transaction. So, you
cannot fetch from a FOR UPDATE cursor after a commit. If you do, PL/SQL raises an
exception. In the following example, the cursor FOR loop fails after the tenth insert:
DECLARE
CURSOR c1 IS SELECT ename FROM emp FOR UPDATE OF sal;
ctr NUMBER := 0;
BEGIN
FOR emp_rec IN c1 LOOP -- FETCHes implicitly
...
ctr := ctr + 1;
INSERT INTO temp VALUES (ctr, ’still going’);
IF ctr >= 10 THEN
COMMIT; -- releases locks
END IF;
END LOOP;
END;
If you want to fetch across commits, do not use the FOR UPDATE and CURRENT OF
clauses. Instead, use the ROWID pseudocolumn to mimic the CURRENT OF clause.
Simply select the rowid of each row into a UROWID variable. Then, use the rowid to
identify the current row during subsequent updates and deletes. An example
follows:
DECLARE
CURSOR c1 IS SELECT ename, job, rowid FROM emp;
my_ename emp.ename%TYPE;
my_job emp.job%TYPE;
my_rowid UROWID;
BEGIN
OPEN c1;
LOOP
FETCH c1 INTO my_ename, my_job, my_rowid;
EXIT WHEN c1%NOTFOUND;
UPDATE emp SET sal = sal * 1.05 WHERE rowid = my_rowid;
-- this mimics WHERE CURRENT OF c1
COMMIT;
END LOOP;
CLOSE c1;
END;
2、禁用自动提交试试。如果你仍然有其他的行在查询的时候也禁用手工提交,当有for update游标仍然打开时执行的任何提交可能会造成这个错误。
setAutoCommit(false)
分享到:
相关推荐
1. ORA-01002:fetch out of sequence - 当游标没有正确地按照执行顺序进行操作时,比如在未调用`OPEN`、`FETCH`或`CLOSE`之前尝试`FETCH`,会出现这个错误。确保在执行`FETCH`前先调用`OPEN`。 2. ORA-00957:...
- 设置`Connection`的自动提交为`false`,避免出现`fetch out of sequence`异常。 - 在查询BLOB数据之前加上`FOR UPDATE`子句,确保锁定包含LOB值的行。 - 获取BLOB对象,并通过`getBinaryOutputStream()`方法...
‘your user_dump_dest‘, ‘read‘) create a wrapper package for utl_file pk_utl_file.sql As TFMADMIN set up the tables, views and sequence tfmadmin_objects.sql create the controlling package pack_...
"checking out paths out of the index and/or a tree-ish to work on advancing the current history" out of the single "git checkout" command. * "git branch --list" learned to always output the ...
A set of transactions can run concurrently if their outputs are disjoint from the union of one another’s input and output sets. For example, if T1 writes some object that is in T2’s input or output ...
You can redirect all of the output of your scripts to a function. For ; example, if you set output_handler to "mb_output_handler", character ; encoding will be transparently converted to the ...
ADC12, Repeated Sequence of Conversions ADC12, Repeated Single Channel Conversions ADC12, Using 10 External Channels for Conversion ADC12, Sequence of Conversions (non-repeated) ADC12, Sample A10 Temp...
- **PostgreSQL** 中,如果需要返回结果集,通常会定义一个返回表类型的函数,使用 `RETURNS SETOF record` 或 `RETURNS TABLE`。 #### 在存储过程中访问或调用函数 - **MS SQL Server** 中可以通过 `EXEC` 或 `...
第一部分 Oracle SQL*PLUS基础 23 第一章 Oracle数据库基础 23 §1.1 理解关系数据库系统(RDBMS) 23 §1.1.1 关系模型 23 ...§18.2.2 创建替代(Instead_of)触发器 300 §18.2.3 创建系统触发器 300...
-- 首先,以超级管理员的身份登录oracle sqlplus sys/bjsxt as sysdba --然后,解除对scott用户的锁 alter user scott account unlock; ... --(默认全局数据库名orcl) 1、select ename, sal * 12 from ...
`NULL`, `NUMBER`, `NUMERIC`, `OBJECT`, `OF`, `OFF`, `OFFSET`, `OLD`, `ON`, `ONLY`, `OPCODE`, `OPEN`, `OPERATOR`, `OPTION`, `OR`, `ORDER`, `ORGANIZATION`, `OUT`, `OVERLAPS`, `OWNER`, `PACKAGE`, `...