hello longcall,
that's just one of the reasons I talk about when I said "more painfull".
I would consider using hours-minutes-seconds-milliseconds rather than the propsed sequence
??????
the sequence is a database object, it's impossible to get 2 same values.
The SEQUENCE is the latest function implemented by DB2 UDB for generating unique values and it needs no extra-code, one INSERT->one generated value.
exemples:
CREATE SEQUENCE MY_SEQUENCE
AS BIGINT
START WITH 1
INCREMENT BY 1
- CREATE SEQUENCE MY_SEQUENCE
- AS BIGINT
- START WITH 1
- INCREMENT BY 1
CREATE TABLE MY_TABLE ( MY_KEY BIGINT NOT NULL,
FIELD1 DATATYPE,
FIELD2 DATATYPE,
...
PRIMARY KEY (MY_KEY))
- CREATE TABLE MY_TABLE ( MY_KEY BIGINT NOT NULL,
- FIELD1 DATATYPE,
- FIELD2 DATATYPE,
- ...
- PRIMARY KEY (MY_KEY))
You will need A sequence for each key in your database of course
when sequence is created, all you have to do is:
INSERT INTO MY_TABLE ( MY_KEY, FIELD1, FIELD2,...)
VALUES(NEXTVAL FOR MY_SEQUENCE, 'value1', 'value2', ...)
- INSERT INTO MY_TABLE ( MY_KEY, FIELD1, FIELD2,...)
- VALUES(NEXTVAL FOR MY_SEQUENCE, 'value1', 'value2', ...)
You can even create a BEFORE iNSERT trigger if you want it to act like an auto-increment datatype like autonumber in ACCESS or "autoincrément" in MYSQL.(you therefore won't even mention the key field in INSERT requests, no need to call nextval, the trigger will do it.)(->just ask me if you're interested in the trigger script)
TO answer to your question, how to get the last key(for example f you need it in your programm script): you can use "PREVVAL FOR MY_SEQUENCE" to get the most recently generated sequence value.
The amazing is that's the returned one is the last one of your own session even if there are many connection session at the same time, doing INSERT opérations
In this chronolgical order:
user1:
INSERT INTO MY_TABLE ( MY_KEY, FIELD1, FIELD2,...)
VALUES(NEXTVAL FOR MY_SEQUENCE, 'valuea1', 'valuea2', ...)
- INSERT INTO MY_TABLE ( MY_KEY, FIELD1, FIELD2,...)
- VALUES(NEXTVAL FOR MY_SEQUENCE, 'valuea1', 'valuea2', ...)
user1 you get 7 from the sequence for MY_KEY value
user2
INSERT INTO MY_TABLE ( MY_KEY, FIELD1, FIELD2,...)
VALUES(NEXTVAL FOR MY_SEQUENCE, 'valueb1', 'valueb2', ...)
- INSERT INTO MY_TABLE ( MY_KEY, FIELD1, FIELD2,...)
- VALUES(NEXTVAL FOR MY_SEQUENCE, 'valueb1', 'valueb2', ...)
User2 get 8 from the sequence for MY_KEY value
user1 again:
SELECT PREVVAL FOR MY_SEQUENCE FROM MY_TABLE
this will return 7 and not 8 to user1 .
then you can do a SELECT * from MY_TABLE WHERE MY_KEY=(SELECT PREVVAL FOR MY_SEQUENCE FROM MY_TABLE) to get your last record inserted (but not the last of any user).
or SELECT * FROM MY_TABLE
WHERE MY_KEY=(SELECT max(MY_KEY) FROM MY_TABLE) to get the very last record.(depending on access, lock, update ryules of course)
The PREVVAL and NEXTVAL values are not impacted by the transaction rollback. when a value in used in an INSERT opération, it won't be generated again even after a rollback.
to me it's a good thing.
A last thing: You can update the way in wich the key is generated at any moment, with ALTER SEQUENCE (new start, increment).