Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Auto increment insert error

Status
Not open for further replies.

khsaunderson

Technical User
Feb 16, 2010
41
GB
In the example below the first 3 columns are the unique identifier, and the 4th column contains the data. The TUNCODE is an auto incrementing field (hence it is commented out).

Code:
INSERT INTO MAIN_TABLE
(--TUNCODE,
TUNTYPE,
TUNCONT,
TUNTITLE)
SELECT 
'1', --TUNTYPE,
'0000000', --TUNCONT,
PHYSICAL_ISRC --TUNTITLE
FROM TEMP_TABLE;

However, every time I run the insert I get an error saying "SQL0803 - Duplicate key value specified." But I have checked and the TEMP_TABLE definitely doesn't contain any duplicates as found in the MAIN_TABLE. So I am beginning to think that it is picking up on the fact that the TUNTYPE and TUNCONT will be duplicated. But since the TUNCODE will be auto incremented, it shouldn't be duplicated. Eg,
Code:
TUNCODE  TUNTYPE  TUNCONT
000012   1        000000
000013   1        000000
000014   1        000000

So I'm very confused...

Any help would be gratefully received.
 

I have answered my own question...

Start by creating a sequence...

Code:
CREATE SEQUENCE TEMP_SEQ  
START WITH 297016  
INCREMENT BY 1  
MINVALUE 297016  
NO MAXVALUE  
CYCLE  
CACHE 4;

Then use the sequence in the insert...

Code:
INSERT INTO MAIN_TABLE
(TUNCODE,
TUNTYPE,
TUNCONT,
TUNTITLE)
SELECT 
NEXTVAL FOR TEMP_SEQ,
'1', 
'0000000', 
PHYSICAL_ISRC
FROM TEMP_TABLE;

Then delete the sequence...

Code:
DROP SEQUENCE TEMP_SEQ RESTRICT;

Hope someone else finds this helpful.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top