I am trying to do multiple inserts into an oracle table with data rom another table and I also use a sequence. Something like this: http://www.dbforums.com/oracle/1626242-insert-into-table-sequence-value-other-table.html

Now..in the destination table there is a primary key on the column that is being populated by the sequence and it is giving me primary key violation. My guess is that the sequence.nextval is not working for some reason. Where is the error? This is my actual query:

    insert into xxxx  (col1, col2, col3, col4, col5)
  select SEQ_CNT.nextval, inner_view.*
    from (select col1, 26, 0, 'N'
    FROM  yyyy WHERE col_ID = 30 AND DELETED = 'N' ) inner_view;
有帮助吗?

解决方案

It seems unlikely to me that the problem is that calling nextval on the sequence is not working. It is much more likely that some other process has inserted data in the table with primary key values greater than the values currently being returned from the sequence. If you

SELECT seq_cnt.nextval
  FROM dual

and compare that to the largest value of the primary key in the table

SELECT max(col1)
  FROM xxxxx

my wager is that the maximum value is greater than the nextval from the sequence. If that's the case, you'd generally want to reset the sequence to the current maximum value as well as figuring out how the problematic data got inserted so that the problem doesn't happen again in the future.

其他提示

the outer query will not loop, hence the sequence will not increment. try this solution below

insert into xxxx  (col1, col2, col3, col4, col5)
  select inner_view.*
    from (select SEQ_CNT.nextval, col1, 26, 0, 'N'
    FROM  yyyy WHERE col_ID = 30 AND DELETED = 'N' ) inner_view;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top