Domanda

We have a java database abstraction that does a number of inserts for us. At runtime we'll know the table name, the column names to insert and the values. From that we generate a prepared statement and do the insert.

In sql server land we would tack on select id = @@identity to the end of the generated sql to get the newly generated id returned by the query.

Now that we're migrating to postgres this no longer works. It's my understanding that in postgres you can do ,

insert into foo(a, b) values('a', 'b') returning ID

Our problem is that at runtime we don't know the name of the ID column nor do we know the name of the sequence. Is there any way to generically get the value of the newly inserted sequence without knowing the name of the sequence or the name of the column?

È stato utile?

Soluzione

If your insert is not triggering further inserts, you can use SELECT LASTVAL(); right after your insert statement

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top