Retrieve inserted row ID in SQL

前端 未结 4 1222
感情败类
感情败类 2020-12-06 02:11

How do I retrieve the ID of an inserted row in SQL?

Users Table:

Column  | Type
--------|--------------------------------
ID      | * Auto-incremen         


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-06 03:09

    In MySQL:

    SELECT LAST_INSERT_ID();
    

    In SQL Server:

    SELECT SCOPE_IDENTITY();
    

    In Oracle:

    SELECT SEQNAME.CURRVAL FROM DUAL;
    

    In PostgreSQL:

    SELECT lastval();
    

    (edited: lastval is any, currval requires a named sequence) Note: lastval() returns the latest sequence value assigned by your session, independently of what is happening in other sessions.

提交回复
热议问题