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 > reset identity value

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-13-07, 11:24
craigmc craigmc is offline
Registered User
 
Join Date: Aug 2003
Location: austin,tx
Posts: 90
reset identity value

- Running db2 8.1 fp 12 on windows

I have a table with an primary key identity column, this table exists on many databases (clients running db2 personal edition). I need to alter the table def, so I want to unload the data, drop the table and recreate it. I need to maintain the key values though for referential integrity.
What I want to do is this:
- CREATE TABLE MyTable_BACKUP LIKE MyTable
- INSERT INTO MyTable_BACKUP SELECT * FROM MyTable
- DROP TABLE MyTable
- <recreate MyTABLE>
- INSERT INTO MyTable SELECT * FROM MyTable_BACKUP

OK, that all works fine.. except the identity start value is now reset back to 1 on the table, so inserting into it inserts a duplicate value.
There is an ALTER TABLE ALTER COLUMN statement for resetting the IDENTITY value, but it only takes a numeric constant, I can't seem to do this:

ALTER TABLE MyTable ALTER COLUMN MyIdentCol RESTART WITH (SELECT MAX(MyIdentCol)+1 FROM MyTable )

Is there a way to reset the next identity value dynamically?

Or any other suggestions?

Thanks for the help.
Reply With Quote
  #2 (permalink)  
Old 03-13-07, 12:02
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
No, you must use a constant for the value. You can create an SQL statement dynamically, then execute that SQL.

Andy
Reply With Quote
  #3 (permalink)  
Old 03-13-07, 12:41
craigmc craigmc is offline
Registered User
 
Join Date: Aug 2003
Location: austin,tx
Posts: 90
I have about 1000 clients, so I need to be able to do all this within a script I will execute on each client. I'm not familar with creating dynamic SQL statements within a DB2 script.
Can that be done, and if so can you point me in the right direction to get started with it?
Reply With Quote
  #4 (permalink)  
Old 03-13-07, 12:48
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Actually, you would create a second script from the first and then execute that.
There are lots of examples on doing this in this forum.

Andy
Reply With Quote
  #5 (permalink)  
Old 03-13-07, 13:07
craigmc craigmc is offline
Registered User
 
Join Date: Aug 2003
Location: austin,tx
Posts: 90
I'll give that a shot, thanks for the help.
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