数据库中 nextVal,currVal的应用.,nextvalcurrval
x.nextVal 得到x序列下一个序列值,x.currVal 得到x序列当前序列值。
注意:在引用 x.currVal之前必须先引用x.nextVal,否则会报ORA-08002: 序列 x.currVal尚未在此会话中定义。
具体应用
1、创建一个序列:
create sequence test_seq increment by 1 start with 1 maxvalue 30 minvalue 0 nocycle cache 10 order;
select sequence test_seq.nextVal from dual;//每调用一次nextVal,序列值会递增一次(递增值自定义)
select sequence test_seq.currVal from dual;
2、创建一个表:
create table test (col1 int, col2 int);
3、插入记录:
insert into test values (0, 0);
4、运用nextVal
insert into test (col1, col2) values (test_seq.nextval, test_seq.nextval);//这里大家猜一下col1和col2值会是一样吗?
假如test表中有两条col1值都为2的记录,那么:
update test set col2 = test_seq.nextval where col1 = 2;//这里大家又猜一下更新后col2值会是一样吗?
本站文章为和通数据库网友分享或者投稿,欢迎任何形式的转载,但请务必注明出处.
同时文章内容如有侵犯了您的权益,请联系QQ:970679559,我们会在尽快处理。