Store generated primary key in temp variable db2

342 Views Asked by At

I'm in the process of writing batch loading SQL script and I would like to store the generated primary key of an insert statement into a temp variable and use it to reference the foreign key in the other tables. Any ideas?

I'm doing the following:

Y = INSERT INTO X(.....)

INSERT INTO Z(...,Y,);
1

There are 1 best solutions below

0
On

I know in SQL Server you can do:

INSERT INTO MyTable ....
SELECT @Foo = @@IDENTITY

INSERT INTO SomeOtherTable(... fk_col ...) VALUES (... @foo ...)