如何使用循环更改 Oracle 序列?
希望有人能帮忙。当我尝试将某些内容插入表中时,出现错误,提示主键已存在。所以我需要重置我的序列,使其始终为 max(id)+1。
该表称为“人员”,有 2 列(ID、姓名)。该序列称为SEQ。
我正在考虑做一个循环。要从 Dual 中选择 SEQ.nextval 运行 n 次。这个 n= max(id)-SEQ.currval
这行得通吗?以及如何将其放入语法中?
多谢。
Hope someone can help. When I tried to insert something into a table it give me error saying the primary key is already existed. So I need to reset my sequence so that it is always max(id)+1.
The table is called 'People' with 2 columns (ID, Name). The sequence is called SEQ.
I am thinking of doing a loop. To run select SEQ.nextval from dual for n times. this n= max(id)-SEQ.currval
Wwill this work? and how Can I put it into the syntax?
Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果这是一次性的,您可以使用更改序列
改变序列sequenceName增量val;
而 val 为最大值+1
然后调用 get nextVal,然后将增量设置回 1。
我将下面的内容放在一起,向您展示如何在不循环的情况下完成它。
或者更好的是,正如 @Dave 建议的那样,只需删除并使用可接受的 开始 值重新创建序列即可。
If this is a one off, you can use the alter sequence
alter sequence sequenceName increment by val ;
whereas val is +1 to the maximum
then call get nextVal, then set the increment back to 1.
I threw the below together to show you how it can be done without looping.
or better yet, as @Dave suggests, just drop and recreate the sequence with the acceptable Start With value.
有了这个,您就可以同步序列,无论它是在 ID 最大值之前还是之后。
只需要更改代码最后的参数即可。
With this one you can synchronize the sequence whatever it is forward or behind the max of the ID.
Just need to change the parameters in the final of the code.