I'm new with DB2 and have this problem: 2+ independ (test) processes are creating new test data records into the same table. To do this I have to fetch the last existing (biggest) ID from this table, increment it and us it for the new record. The smallest ID has to be 1000 for some reasons. There is no sequence belonging to the ID columnd, nor any other automatic mechanism, but it hast to be unique of course.
I wrote this SQL statement:
SELECT ID FROM FINAL TABLE(
INSERT INTO table_x (ID,NAME)
VALUES((SELECT COALESCE(MAX_ID, 1000) MAX_ID FROM (
SELECT * FROM (SELECT MAX(ID) AS MAX_ID FROM table_x))
)+1,'Ben')
)
The statement works, but it happens running this statements multiple times at the time there is still a concurrency issue because identical ID.
Obviously I have to do something more. I guess table locking could be a solution or maybe there are other best practices? I found some articles about NEXTVAL, but it works only with sequences.
Yes, I found this DB2 INSERT INTO … SELECT lock but I didn't really see a solution for my case.
Thanks for any hints!