therufa
therufa

Reputation: 2041

Get data from the last insert query using postgres

i know there is a way how i can get the whole data from my last insert, including the auto generated fields, like id, and default content. But the problem is: How can i do this?

for eg:

INSERT INTO schema.table (col1,col2) VALUES ("rowdata1","rowdata2");

the table looks like this:

id, col1, col2, col3 (default='t')

so how can i get the value of id and col3? There's a keyword like RETURNING or so, but this throws an error :)

Upvotes: 2

Views: 416

Answers (1)

Quassnoi
Quassnoi

Reputation: 425803

INSERT
INTO    schema.table (col1, col2)
VALUES  ('rowdata1', 'rowdata2')
RETURNING
        *

, or, if you only need specific columns,

INSERT
INTO    schema.table (col1, col2)
VALUES  ('rowdata1', 'rowdata2')
RETURNING
        id, col3

Upvotes: 4

Related Questions