INSERT, and get the auto-incremented value

后端 未结 3 1513
梦毁少年i
梦毁少年i 2021-01-20 15:22

Consider the following table:

create table language (
    id integer generated always as identity (START WITH 1, INCREMENT BY 1),
    name long varchar,
             


        
3条回答
  •  南方客
    南方客 (楼主)
    2021-01-20 15:48

    You could execute this statement (NB, not 100% sure this syntax is correct for Derby:

    SELECT TOP 1 id FROM language ORDER BY id DESC
    

    To find the last inserted ID.

    Alternative for Derby:

    SELECT MAX(id) from language
    

    Obviously this will only be accurate if no other inserts (including inserts by other users) have happened between your insert and select.

    See also this discussion:

提交回复
热议问题