IBMi (AS400) fans only : Table with self-incrementing primary key and current time field
#IBMiSample
How to get a primary key field that increments itself and a time field that updates itself?
Look at LGID00 and LGDTTM fields.
Create a table:
CREATE TABLE LGCUS00F
(
LGID00 INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
LGDTTM TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
LGNAME CHAR(50),
LGSURN CHAR(50))
RCDFMT LGCUS
Create a table:
CREATE TABLE LGCUS00F
(
LGID00 INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
LGDTTM TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
LGNAME CHAR(50),
LGSURN CHAR(50))
RCDFMT LGCUS
Now try to add a record:
INSERT INTO LGCUS00F (LGNAME, LGSURN)
VALUES ('Aldo', 'Succi')
Let's see the result:
SELECT * FROM LGCUS00F
As you can see, LGID00=1 and LGDTTM=2018-04-01-10.01.49.781922
That's it!
I appreciate all the comments made on this blog.
Is this possible when creating a new pf?
ReplyDeleteI don't know if self-incrementing fields can be created through the definition of DDS. I prefer to use SQL to define a new pf.
DeleteCan anyone answer this question?
No, there is no way to do it thru a DDS, only using SQL>
DeleteHow abiut create tabel from design?
ReplyDeleteHi Fatra, I don't know this thechnique. Can you explain me?
DeleteRhe jnteger generated, is it possible to use this function in the select statemet.
ReplyDeleteThe idea is to query certain file and add new incremental value not the rrn though