SQL - INSERT and catch the id auto-increment value

前端 未结 7 958
梦毁少年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:52

    It depends on the database engine you are using. Some DBMS, like Firebird for example, have RETURNING clause you can add to your query. For example, if you have a table named TABLE1 with autoincrement column named ID, you can use this:

    insert into TABLE1(columns...) values (values...) returning ID;
    

    And it would return the inserted ID just like a regular select statement.

提交回复
热议问题