Whats the standard way of getting the last insert id?

后端 未结 4 1285
刺人心
刺人心 2021-01-05 05:32

What\'s the sql standard to get the last inserted id? If there is such a thing.

mysql: LAST_INSERT_ID()
postgresql: ... RETURNING f_id
mssql: SCOPE_IDENTIT

4条回答
  •  一个人的身影
    2021-01-05 06:05

    The one technique is likely to work with all DBs, and in circumstances where the sequence is not a simple incrementing number (e.g. there are pre-existing rows with high ids, so you can't use MAX), is:

    1. Determine what the next id will be, e.g. using nextval function or similar.
    2. Insert the row using that id.
    3. Use the id for the rest of your needs.

提交回复
热议问题