2013-02-26 251 views
0

我正在嘗試使用另一個表中的數據執行多個插入到oracle表中,我也使用一個序列。像這樣:http://www.dbforums.com/oracle/1626242-insert-into-table-sequence-value-other-table.htmloracle批量插入失敗,因爲序列不會自動增加

Now..in the destination table there are a primary key on the column that being populated by the sequence and it is given me primary key violation。我的猜測是,sequence.nextval由於某種原因不起作用。錯誤在哪裏?這是我的實際查詢:

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; 
+0

「col1」是您的主鍵嗎?一個可以獲得序列值的人?我問,因爲你有一個col1'在你的inner_view以及 – 2013-02-26 17:27:07

+0

是col1是主鍵 – Victor 2013-02-26 18:55:23

回答

1

這似乎不太可能,我認爲問題是,在程序調用nextval不工作。更有可能的是,某些其他進程在表中插入了主鍵值大於當前從序列返回的值的數據。如果您

SELECT seq_cnt.nextval 
    FROM dual 

和比較,主鍵的最大價值在表

SELECT max(col1) 
    FROM xxxxx 

我的賭注是,最大值小於nextval從序列更大。如果是這種情況,通常需要將序列重置爲當前的最大值,並確定如何插入有問題的數據,以便將來不會再發生問題。

+0

謝謝甲骨文上帝先生洞穴 – Victor 2013-02-26 18:55:45

0

外部查詢不會循環,因此序列不會增加。請嘗試下面的解決方案

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;