Accessing the value of an Identity column after insert
Posted: Mon Jul 30, 2018 10:34 am
Any ideas on obtaining the value assigned to an Identity column (DB2 table) after an insert? There is no natural key to query on. The value is needed to use in subsequent logic. My coworkers have no experience in using Identity columns and seem to think DS doesn't handle them at all.
IBM doc on the topic of using Identity columns: https://www.ibm.com/support/knowledgece ... ycols.html
In regular SQL the value can be obtained using code like below (assuming a three column table called SOMETABLENAME with columns called: ID_COL_FIELD, FIELD1, FIELD2 with ID_COL_FIELD being the identity column). After the call the variable W-ID_COL_FIELD would contain the value assigned during the insert. An alternative is to use function IDENTITY_VAL_LOCAL() to obtain the value assigned in the most recent insert.
IBM doc on the topic of using Identity columns: https://www.ibm.com/support/knowledgece ... ycols.html
In regular SQL the value can be obtained using code like below (assuming a three column table called SOMETABLENAME with columns called: ID_COL_FIELD, FIELD1, FIELD2 with ID_COL_FIELD being the identity column). After the call the variable W-ID_COL_FIELD would contain the value assigned during the insert. An alternative is to use function IDENTITY_VAL_LOCAL() to obtain the value assigned in the most recent insert.
Code: Select all
SQL SELECT ID_COL_FIELD INTO :W-ID_COL_FIELD
FROM FINAL TABLE
( INSERT INTO SOMETABLENAME
( FIELD1, FIELD2)
VALUES( :W-FIELD1, :W-FIELD2 ) )