SQL - INSERT and catch the id auto-increment value

前端 未结 7 957
梦毁少年i
梦毁少年i 2020-12-05 19:26

What is the best way to get the auto-id value in the same SQL with a SELECT?

A forum said adding this \"; has Return Scope_Identity()\"
in the end

7条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-05 19:49

    It depends on your database server. Using MySQL, call mysql_insert_id() immediately after your insert query. Using PostgreSQL, first query "select nextval(seq)" on the sequence and include the key in your insert query.

    Querying for "select max(id) + 1 from tbl" could fail if another request inserts a record simultaneously.

提交回复
热议问题