SQL SERVER – @@IDENTITY vs SCOPE_IDENTITY() vs IDENT_CURRENT – Retrieve Last Inserted Identity of Record

SELECT @@IDENTITY
It returns the last IDENTITY value produced on a connection, regardless of the table that produced the value, and regardless of the scope of the statement that produced the value.
@@IDENTITY will return the last identity value entered into a table in your current session. While @@IDENTITY is limited to the current session, it is not limited to the current scope. If you have a trigger on a table that causes an identity to be created in another table, you will get the identity that was created last, even if it was the trigger that created it.

SELECT SCOPE_IDENTITY()
It returns the last IDENTITY value produced on a connection and by a statement in the same scope, regardless of the table that produced the value.
SCOPE_IDENTITY(), like @@IDENTITY, will return the last identity value created in the current session, but it will also limit it to your current scope as well. In other words, it will return the last identity value that you explicitly created, rather than any identity that was created by a trigger or a user defined function.

SELECT IDENT_CURRENT(‘tablename’)
It returns the last IDENTITY value produced in a table, regardless of the connection that created the value, and regardless of the scope of the statement that produced the value.
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.

To avoid the potential problems associated with adding a trigger later on, always use SCOPE_IDENTITY() to return the identity of the recently added row in your T SQL Statement or Stored Procedure.

Reference : Pinal Dave (https://blog.sqlauthority.com)

SQL Function, SQL Index, SQL Scripts
Previous Post
SQL SERVER – Stored Procedure – Clean Cache and Clean Buffer
Next Post
SQL SERVER – Fix : Error 701 There is insufficient system memory to run this query

Related Posts

230 Comments. Leave new

  • You saved me again. Your posts always provide better explanations than MS

    Reply
  • Following SQL Query is to get the LAST INSERTED ID if the column is auto increment using iBatis and SQL SERVER.

    — your insert statement

    select @@IDENTITY as value

    Reply
  • Chucksterlicious
    October 10, 2009 2:39 am

    I think it’s important to note here that while @@IDENTITY and SCOPE_IDENTITY() appear to work the same in the context of the same scope, that only @@IDENTITY will work if you happen to be trying to reference the current identity value within the same insert statement that is creating the IDENTITY value. SCOPE_IDENTITY() will return a NULL value in this case. I know you will ask why you would ever want to do this, but i just had a case where i had to do this and SCOPE_IDENTITY() did not work.

    Reply
  • I’m convinced with the use of SCOPE_IDENTITY until one scenario i found out that SCOPE_IDENTITY doesn’t return the desired value if Instead of Triggers is implemented on the desired table.

    But luckily, the @@IDENTITY works with that way.

    However, still can’t understand the logic behind the scenario.

    (Scenario Notes:)

    INSTEAD OF TRIGGERS => checks the other tables in terms of required data, then, reinserts all fields with updated data.

    Reply
    • Sanjay Monpara
      August 12, 2015 12:15 pm

      I also found similar issue, but @@Identity is also risky in case you are inserting a record in another table via trigger.

      here you can use current_ident(tablename), but I have doubt, what happen if two persons try to insert a record in a same table and same time?

      Reply
  • @Ieuan0928

    Could you post the scenario please. That is odd that id did not work.

    Reply
  • Hello,

    I have the same question like raksha posted
    on July 9, 2007 at 2:07 am

    Can you please help us?

    Thanks and Regards,
    Sandhya P.

    Reply
  • Good explanations.

    In one of my application SCOPE_IDENTITY() returns 1, some times. Below is the Code

    INSERT INTO CUSTOMER(…)
    SELECT … FROM #temp_table

    SET @id = SCOPE_IDENTITY()

    Note: Here CUSTOMER table has a AFTER trigger.

    Any Guess..?

    Reply
  • Thanks for producing a nice short article that gave the answer to what I was looking for.

    Reply
  • how i know latest updated and inserted records in sql table.

    Reply
  • Sandhya M. Potdar
    January 4, 2010 3:19 pm

    Hi,

    SET @id = SCOPE_IDENTITY() was the solution.

    Thank you.

    Regards,
    Sandhya M. Potdar.

    Reply
  • Again good article….

    Reply
  • SCOPE_IDENTITY() is useful when multiple user inserting data in same table at same time??
    so that will get separate identity for each row inserted.
    ??
    or will get mess up

    Reply
  • Jacob Sebastian
    January 28, 2010 6:11 pm

    No, SCOPE_IDENTITY does not get messed up when multiple users insert data. You will get the identity value generated by your session only.

    Reply
  • Thankx a lot

    Reply
  • Vinaykumar Srivastava
    February 19, 2010 8:23 pm

    Thanks a lot. Thearticle was very useful. Keep writing such articles as it is very useful for Developer community…

    Reply
  • Hi,

    I am inserting multiple record. i am getting only last record id.i need latest inserted record id.
    SCOPE_IDENTITY() it’s givining only 1 record.

    example: I inserted 3 record,i need 3 records id.

    Create PROCEDURE [dbo].[OrganisationSelectedType_insert]
    (
    @prmOrgTypeID nvarchar(50),
    @prmOrganisationID nvarchar(50),
    @prmOrganisationName nvarchar(50)

    )
    AS
    BEGIN
    INSERT INTO [OrganisationTypes] ([OrganisationID],[OrganisationName],[OrgTypeID])
    SELECT @prmOrganisationID,@prmOrganisationName, data FROM [Split] (@prmOrgTypeID, ‘;’)

    SELECT SCOPE_IDENTITY() As savedId
    END

    please help.

    Thanks

    Reply
  • Thanks for useful info…

    I needed this for a long time for improving efficiency of my applications.

    Thanks again…

    Reply
  • Thansk for this article.it help me to solve my problem
    thanks

    Reply
  • I have a table of 25 records. Client mistakenly deleted the 15th row. Can u able to insert the same row with same identity? If possible how?

    Reply
  • Could U Plz help me ….

    Reply

Leave a Reply