How can I get the ID of the last INSERTed row using PDO with SQL Server?

前端 未结 2 415
故里飘歌
故里飘歌 2021-02-08 16:19

Under other circumstances I might be tempted to use

$result = mssql_query(\"INSERT INTO table (fields) VALUES (data); 
                       SELECT CAST(scope_i         


        
2条回答
  •  别那么骄傲
    2021-02-08 16:53

    You've got a few choices:

    SELECT @@IDENTITY - return the last ID created by actions of the current connection, regardless of table/scope
    
    SELECT SCOPE_IDENTITY() - last ID produced by the current connection, in scope, regardless of table
    
    SELECT IDENT_CURRENT('name_of_table'); - last ID produced on that table, regardless of table/scope/connection
    

    Of the three, SCOPE_IDENTITY() is the best candidate.

提交回复
热议问题