Weren't you the one who is switching to DDL vs DDS? If so then define
this file as
CREATE TABLE ROB/DOUGX
(THEKEY INT as identity,
... more columns here
CONSTRAINT DOUGX_PRIMARYKEY PRIMARY KEY (THEKEY))
and THEKEY will automatically increment.
The trick is to not write out all columns. For example,
when you do an SQL INSERT do not specify THEKEY as one of the fields,
when you do an RPG write or update use %fields and do not specify THEKEY.
There are ways to retrieve the last new key used. I'd have to look those
up if you choose to go that way.

Alternate plan.
ADDPFCST FILE(ROB/DOUGX) TYPE(*UNQCST) KEY(THEKEY)
CST(DOUGX_THEKEY_UNIQUE)

One way to assign the next key is by a SEQUENCE
STRSQL
CREATE SEQUENCE MYLIB/MYSEQ.
Basically it creates a data area and you can use SQL to retrieve the
sequence number and have it incremented - RTM. You can also access via
typical RPG data area commands.

However!!!!!
I recommend that you store the technique to GetNextNumber in a
subprocedure, preferably in a service program. This will allow you to
change your method at a whim and not have to recompile any existing
program. For example, let's say you stick with the read highest key
method. And you decide that causes issues because you never defined RI on
your files and someone created a child record of the highest number (let's
say 99). Someone deleted 99 out of the parent but not out of the child.
So you create a new 99 because 98 was then the highest number. Now, the
new 99 already has children. If you use the service program technique you
can change the methodology and not have to recompile programs in
production. Also the "highest key" method may have issues in a high
volume environment where both people read the file and get 99 as the next
highest key and try to write their records out.


Rob Berendt

As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.