Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Qlik and ServiceNow Partner to Bring Trusted Enterprise Context into AI-Powered Workflows. Learn More!
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

How to get last inserted id in DB2?

Hello,
Anyone has an idea on how to get last inserted id in DB2?
I have two tables in db2, one child, one parent. I would like to insert data into one table, and get the id (which auto generated in the db2,) then insert the id into another table as a foreign key.
Thanks.
Labels (2)
4 Replies
Anonymous
Not applicable
Author

The best way is to get the new id first with a tDB2Row (within a flow + tParseRecordSet) and use this new id directly.
All other ways are not really reliable.
Anonymous
Not applicable
Author

Hi jlolling,
Thanks for taking time to answer my question.
Do you mean get the new id before insert any data into the first table? My table is an existing table, and id is designed to auto-generated by database. I don't think I can change it now.
If I misunderstood your meaning, could you please explain a little bit more.
Thanks!
Anonymous
Not applicable
Author

In this case you cannot follow my last advise. 
Ok try this:
Within the same connection and same transaction use tDB2Input with this select:
SELECT SYSIBM.IDENTITY_VAL_LOCAL() AS id FROM my_schema.my_table

Hope it works. I have not tested it yet.
Anonymous
Not applicable
Author

Yes. I used this one. It works.
SELECT Identity_val_Local() as  id FROM sysibm.sysdummy1
Thanks for giving me the clue, jlolling.