Best way to get identity of inserted row?

后端 未结 14 2413
醉梦人生
醉梦人生 2020-11-21 07:06

What is the best way to get IDENTITY of inserted row?

I know about @@IDENTITY and IDENT_CURRENT and SCOPE_IDENTITY

相关标签:
14条回答
  • 2020-11-21 07:48

    Add

    SELECT CAST(scope_identity() AS int);
    

    to the end of your insert sql statement, then

    NewId = command.ExecuteScalar()
    

    will retrieve it.

    0 讨论(0)
  • 2020-11-21 07:49

    ALWAYS use scope_identity(), there's NEVER a need for anything else.

    0 讨论(0)
  • 2020-11-21 07:50

    Even though this is an older thread, there is a newer way to do this which avoids some of the pitfalls of the IDENTITY column in older versions of SQL Server, like gaps in the identity values after server reboots. Sequences are available in SQL Server 2016 and forward which is the newer way is to create a SEQUENCE object using TSQL. This allows you create your own numeric sequence object in SQL Server and control how it increments.

    Here is an example:

    CREATE SEQUENCE CountBy1  
        START WITH 1  
        INCREMENT BY 1 ;  
    GO  
    

    Then in TSQL you would do the following to get the next sequence ID:

    SELECT NEXT VALUE FOR CountBy1 AS SequenceID
    GO
    

    Here are the links to CREATE SEQUENCE and NEXT VALUE FOR

    0 讨论(0)
  • 2020-11-21 07:52

    I can't speak to other versions of SQL Server, but in 2012, outputting directly works just fine. You don't need to bother with a temporary table.

    INSERT INTO MyTable
    OUTPUT INSERTED.ID
    VALUES (...)
    

    By the way, this technique also works when inserting multiple rows.

    INSERT INTO MyTable
    OUTPUT INSERTED.ID
    VALUES
        (...),
        (...),
        (...)
    

    Output

    ID
    2
    3
    4
    
    0 讨论(0)
  • 2020-11-21 07:54

    MSDN

    @@IDENTITY, SCOPE_IDENTITY, and IDENT_CURRENT are similar functions in that they return the last value inserted into the IDENTITY column of a table.

    @@IDENTITY and SCOPE_IDENTITY will return the last identity value generated in any table in the current session. However, SCOPE_IDENTITY returns the value only within the current scope; @@IDENTITY is not limited to a specific scope.

    IDENT_CURRENT is not limited by scope and session; it is limited to a specified table. IDENT_CURRENT returns the identity value generated for a specific table in any session and any scope. For more information, see IDENT_CURRENT.

    • IDENT_CURRENT is a function which takes a table as a argument.
    • @@IDENTITY may return confusing result when you have an trigger on the table
    • SCOPE_IDENTITY is your hero most of the time.
    0 讨论(0)
  • 2020-11-21 07:58

    I'm saying the same thing as the other guys, so everyone's correct, I'm just trying to make it more clear.

    @@IDENTITY returns the id of the last thing that was inserted by your client's connection to the database.
    Most of the time this works fine, but sometimes a trigger will go and insert a new row that you don't know about, and you'll get the ID from this new row, instead of the one you want

    SCOPE_IDENTITY() solves this problem. It returns the id of the last thing that you inserted in the SQL code you sent to the database. If triggers go and create extra rows, they won't cause the wrong value to get returned. Hooray

    IDENT_CURRENT returns the last ID that was inserted by anyone. If some other app happens to insert another row at an unforunate time, you'll get the ID of that row instead of your one.

    If you want to play it safe, always use SCOPE_IDENTITY(). If you stick with @@IDENTITY and someone decides to add a trigger later on, all your code will break.

    0 讨论(0)
提交回复
热议问题