PostgreSQL function for last inserted ID

后端 未结 11 1348

In PostgreSQL, how do I get the last id inserted into a table?

In MS SQL there is SCOPE_IDENTITY().

Please do not advise me to use something like this:

11条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 14:09

    You can use RETURNING id after insert query.

    INSERT INTO distributors (id, name) VALUES (DEFAULT, 'ALI') RETURNING id;
    

    and result:

     id 
    ----
      1
    
    

    In the above example id is auto-increment filed.

提交回复
热议问题