user1022900
user1022900

Reputation: 13

Store generated primary key in temp variable db2

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,);

Upvotes: 0

Views: 335

Answers (1)

Dylan Smith
Dylan Smith

Reputation: 22245

I know in SQL Server you can do:

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

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

Upvotes: -1

Related Questions