You can do the following:
1. Retrieve the CREATE TABLE Statement (with the ACS Wizard)
2. Modify the GENERATED ALWAYS for the Identity Column to GENERATE BY DEFAULT
3. After you will be able to change the (duplicate) Identity values.
4. I'd suggest to CREATE a SEQUENCE which starts with a value higher than the maximum Id.
5. After you can run an UPDATE Statement replacing the duplicate Ids out of the SEQUENCE
6. Change the GENERATED BY DEFAULT clause back to GENERATED ALWAYS, rerun your CREATE OR REPLACE TABLE Statement and Restart the Identity counter
The following example works for me:
Create OR Replace Table Hscommon05.Testid (
Id Integer Generated Always As Identity (Start With 1, Increment By 1,
No Order, No Cycle,
No Minvalue, No Maxvalue, Cache 20)
Not Null Not Hidden,
Text Varchar(10) Default '' Not Null Not Hidden);
Insert into HSCOMMON05.TestId (Text) Values('XXX'), ('YYY'), ('ZZZ'), ('WWW');
Alter Table Hscommon05/Testid Alter Column Id Restart With 3;
Insert into HSCOMMON05.TestId (Text) Values('AAA'), ('BBB');
Create or Replace Sequence Hscommon05.Seqtestid
As Integer Start With 10 ;
Create OR Replace Table Hscommon05.Testid (
Id Integer Generated By Default As Identity (Start With 1, Increment By 1,
No Order, No Cycle,
No Minvalue, No Maxvalue, Cache 20)
Not Null Not Hidden,
Text Varchar(10) Default '' Not Null Not Hidden);
Update HSCOMMON05.TestId b
set Id = Next Value For HSCOMMON05.SeqTestId
Where rrn(b) in (Select Max(rrn(a))
from HSCOMMON05.TestId a
Group by Id
Having Count(*) > 1);
Create OR Replace Table Hscommon05.Testid (
Id Integer Generated Always As Identity (Start With 20, Increment By 1,
No Order, No Cycle,
No Minvalue, No Maxvalue, Cache 20)
Not Null Not Hidden,
Text Varchar(10) Default '' Not Null Not Hidden);
Insert into HSCOMMON05.TestId (Text) Values('EEE'), ('FFF');
------------------------------
Birgitta Hauser
Database and Software Engineer
Selfemployed - Modernization-Education-Consulting on IBM i
Kaufering
+49 170 5269964
------------------------------
Original Message:
Sent: Tue August 09, 2022 08:58 AM
From: Patrick Conner
Subject: Clean up identity to add unique key and debug
We have a table that was defined with the natural key as a primary key even though the table was defined with an identity column. No constraint was added to keep the identity column unique. Now, the identity column has duplicates and I've been asked to clean it up before adding a unique constraint. I'm sure someone has run into this before. Has anyone created a stored procedure to fix? If so, can you share your code or point to a web example?
Also, how are stored procedures debugged? Any videos available on the web?
------------------------------
Patrick Conner
------------------------------
#SQL