我在一个事务里取序列值nextval 为id,假如=1,然后再根据这个插入的id,取出此条记录,会出现别人插入了新的记录,取出来的不是自己插入的那条的情况吗?也就是说id=2的记录被查出来.insert into t values( seq.nextval ,name)...select * from t where id=(select max(id) from t);不是很明白,请高手指点一下.
当然有有可能啊。 别的事务在你insert into t values( seq.nextval ,name)... 了后,把事务提交了。 然后你又select * from t where id=(select max(id) from t); 这个时候你看到的就是别人的数据。而不是id=2的数据。
解决办法: declare cc int; max_v int; begin select seq.nextval into cc from dual; insert into tt valuse(cc); select max(colN) into max_v from tt; if max_v=cc then dbms_output.put_line('没有错,可以继续进行!'); else dbms_output.put_line('有错,不可以继续进行!'); end if; end; /当然你也可以直接lock表,但不推荐,因为影响并行操作。
别的事务在你insert into t values( seq.nextval ,name)... 了后,把事务提交了。
然后你又select * from t where id=(select max(id) from t); 这个时候你看到的就是别人的数据。而不是id=2的数据。
declare
cc int;
max_v int;
begin
select seq.nextval into cc from dual;
insert into tt valuse(cc);
select max(colN) into max_v from tt;
if max_v=cc then
dbms_output.put_line('没有错,可以继续进行!');
else
dbms_output.put_line('有错,不可以继续进行!');
end if;
end;
/当然你也可以直接lock表,但不推荐,因为影响并行操作。