Quantcast
Channel: SCN : All Content - All Communities
Viewing all articles
Browse latest Browse all 3363

How can I reset the sequence to max ID after inserts to the table with key?

$
0
0

Consider this scenario;

 

You have a table and a primary key within it created as follows as part of your create statement;

 

CREATE COLUMN TABLE "TABLE1" (

  "ID" BIGINT GENERATED BY DEFAULT AS IDENTITY(START WITH 1 INCREMENT BY 1)

  ,"NAME" NVARCHAR(255) NOT NULL

  ,PRIMARY KEY ("ID")

  );

 

Let's say during the initial data load, you have added rows giving the ID value in your insert statement due to dependencies on other tables and foreign keys using this table like below;

 

INSERT INTO TABLE1 (ID, NAME) VALUES (1, "Key 1");

INSERT INTO TABLE1 (ID, NAME) VALUES (2, "Key 2");


Then if you try to insert new rows in future without the ID value, you get an error saying "[301]: unique constraint violated:". You know the issue as the sequence number is not incremented when you added the rows providing the key.


So, your only option the way I see it is the either not give the ID as part of the input to let the system handle it or not use the IDENTITY at all and create your own sequence which you can manipulate later. Because these system generated sequences via IDENTITY feature is not possible to alter. When you try to reset (alter sequence "SYSTEM"."_SYS_SEQUENCE_1527547_#0_#" restart with 3), you get an error like below;


"SAP DBTech JDBC: [383]: invalid identifier: _sys_ prefix not allowed: "


That is of course after finding the name of your sequence via;


select '"' || SCHEMA_NAME || '"."' || SEQUENCE_NAME || '"' AS SEQUENCE_NAME from SEQUENCES

WHERE RESET_BY_QUERY LIKE '%TABLE1%';


So, first of all, is there a way to reset the sequence to the max ID in the table after the inserts? If not, what would be your recommendation to overcome this issue? How do you access the current or next value in the sequence in these auto generated sequences?


Thanks in advance.



Viewing all articles
Browse latest Browse all 3363

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>