If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Copying tables with identity columns between DBs

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-29-04, 12:38
patlv23 patlv23 is offline
Registered User
 
Join Date: May 2002
Location: Philippines
Posts: 11
Question Copying tables with identity columns between DBs

Hi,

We are trying to copy data between databases. However we are having problems with tables with columns "generated by default as identity". Once data is transferred to another DB, the identity column doesn't detect that data already exists, thus causing a duplicate key error on the next insert. It would have been ok to just regenerate these values but they are linked to other tables as foreign keys.

How could I work around this? Is there a way to set the "next available value" for identity columns?

One way I thought of is to determine the MAX value of that column to be inserted, then insert that number of "dummy" records into the new table. Delete the "dummy" records, then insert/import the real data. A duplicate key error should no longer appear. However this seems like an extreme solution.

Thanks in advance
Reply With Quote
  #2 (permalink)  
Old 09-29-04, 13:08
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Use:
ALTER TABLE mytable ALTER COLUMN identityCol restart with x

where x is max+1 of current identity values.

HTH

Andy
Reply With Quote
  #3 (permalink)  
Old 09-30-04, 06:12
patlv23 patlv23 is offline
Registered User
 
Join Date: May 2002
Location: Philippines
Posts: 11
Thumbs up

Quote:
Originally Posted by ARWinner
Use:
ALTER TABLE mytable ALTER COLUMN identityCol restart with x

where x is max+1 of current identity values.

HTH

Andy
Thanks Andy, that's exactly what I needed!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On