Db2: How to update the current value of a sequence
Asked Answered
G

3

9

We use a sequence in a Db2 database. Recently, we have migrated the data from an AIX server to a Linux server. During that the latest number of that sequence was not moved to the Linux system. As a consequence, we are seeing duplicates values now.

Here is how we use the sequence:

SELECT NEXTVAL FOR SEQ_YFS_ORDER_NO FROM SYSIBM.SYSDUMMY1

The current value of the sequence on Linux is 100092142. How can I update it to the current value that we have on the AIX system, i.e to (100110960)?

Gracie answered 31/1, 2018 at 6:12 Comment(0)
S
15

You can modify the sequence using ALTER SEQUENCE. An option offered by ALTER SEQUENCE is to RESTART it with a specific value. Try something like this:

ALTER SEQUENCE SEQ_YFS_ORDER_NO RESTART WITH 100110960

Also note that sequence numbers typically are cached. This may lead to a gap and could have caused the issue during the migration.

Smallish answered 31/1, 2018 at 6:34 Comment(0)
D
2

Use the below query to fetch next sequence value from DB2 database.

  SELECT NEXT VALUE FOR "Sequence_name"  FROM SYSIBM.SYSDUMMY1
Distaff answered 7/12, 2018 at 10:7 Comment(0)
P
0

Work Around

ALTER SEQUENCE SEQ_YFS_ORDER_NO INCREMENT BY (100110960-100092142);
SELECT SEQ_YFS_ORDER_NO FROM dual;
ALTER SEQUENCE SEQ_YFS_ORDER_NO INCREMENT BY 1;

Before backup of your database, you can too create one Stored Procedure to SET "START WITH" with current_value to all your SEQUENCES, so when you will do restore, they will be restored with your desired starts.

Pinworm answered 22/7, 2021 at 22:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.