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

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.

Comments

  1. Is this possible when creating a new pf?

    ReplyDelete
    Replies
    1. I 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.

      Can anyone answer this question?

      Delete
    2. No, there is no way to do it thru a DDS, only using SQL>

      Delete
  2. How abiut create tabel from design?

    ReplyDelete
    Replies
    1. Hi Fatra, I don't know this thechnique. Can you explain me?

      Delete
  3. Rhe jnteger generated, is it possible to use this function in the select statemet.
    The idea is to query certain file and add new incremental value not the rrn though

    ReplyDelete

Post a Comment

Popular posts from this blog

(IBM i fans only) Efficient WRKSPLF with WSF - How to Search string into spooled files, Sort, and Generate PDFs on IBMi

(IBM i fans only) Detecting and Handling Non-Printable Characters in DB2 SQL Using LOCATE() and REPLACE() Functions

(IBM i fans only) How to Sniff User Access