Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Db2: How to update the current value of a sequence

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)?

like image 513
Sivapandian Jayakumar Avatar asked Jan 31 '18 06:01

Sivapandian Jayakumar


People also ask

How do you find the current value of a sequence in db2?

You can access the value of a sequence using the NEXTVAL or CURRVAL operators in SQL statements. You must qualify NEXTVAL or CURRVAL with the name (or synonym) of a sequence object that exists in the same database, using the format sequence. NEXTVAL or sequence. CURRVAL.

How can I change the current value of a sequence in SQL Server?

Sequences are integer values and can be of any data type that returns an integer. The data type cannot be changed by using the ALTER SEQUENCE statement. To change the data type, drop and create the sequence object.

Can we modify a sequence?

Oracle ALTER SEQUENCE OverviewThe ALTER SEQUENCE statement allows you to change the increment, minimum value, maximum value, cached numbers, and behavior of a sequence object. All the parameters have the same meaning as described in the CREATE SEQUENCE statement.

Does Nextval increment the sequence?

The first reference to NEXTVAL returns the sequence's initial value. Subsequent references to NEXTVAL increment the sequence value by the defined increment and return the new value.


2 Answers

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.

like image 181
data_henrik Avatar answered Sep 23 '22 17:09

data_henrik


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

  SELECT NEXT VALUE FOR "Sequence_name"  FROM SYSIBM.SYSDUMMY1
like image 44
Abhishek Jha Avatar answered Sep 24 '22 17:09

Abhishek Jha