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

  • Imran Mohammed
    March 11, 2009 7:33 am

    @Harinath,

    Were you able to log into SQL Server and you cannot access database ?

    Please post exact error message.

    What is state of database, is it online /suspect/restoring ?

    Regards,
    IM

    Reply
  • ANIL KUMAR DUBEY
    March 26, 2009 8:27 pm

    Hi,

    I need to know, when i should use @@identity or scope_identity() inside any scope. What criteria i should keep in mind when i have to use any one of them in my program.

    Thanks

    Anil kumar dubey

    Reply
  • Great post. Exactly what I was looking for.

    Reply
  • i have used

    SELECT IDENT_CURRENT(dbo.ITA_STORICO_ORDINI_DISTINTA)

    but i have received this error

    Msg 4104, Level 16, State 1, Line 1
    The multi-part identifier “dbo.ITA_STORICO_ORDINI_DISTINTA” could not be bound.

    ??????

    Reply
  • SELECT CONVERT(INT, SCOPE_IDENTITY()) AS ID

    will return the scope identity in for OdbcCmd object. Just doing a SELECT SCOPE_IDENTITY() results type cast errors if you are trying to get the ID out like this

    ID = (int) cmd.ExecuteScalar();

    Reply
  • Imran Mohammed
    May 15, 2009 7:56 am

    @Mauro

    Table name should be in single quotations like this,

    SELECT IDENT_CURRENT(‘dbo.ITA_STORICO_ORDINI_DISTINTA’)

    Try this, Should work.

    IM.

    Reply
  • hello ..
    i am trying to make a sp how looks something like this,
    what i want is to be able to get 2tables to create a new customer. .. i dosent work.. can you help me???

    @Namn varchar(40),
    @Adress varchar(50),
    @Postnr varchar(6),
    @Beskrivning varchar(max),
    @Erbjudande varchar(max),
    @kontaktUppgift varchar(30),
    @CafeId int

    AS
    BEGIN
    SET NOCOUNT ON;

    BEGIN TRAN
    declare @CafeId int
    BEGIN TRY

    INSERT INTO app.Cafe(Namn, Adress, PostNr, Beskrivning, Erbjudande)
    VALUES(@Namn, @Adress,@Postnr,@Beskrivning,@Erbjudande)

    Insert into app.Kontakt ( kontaktUppgift , CafeID)
    values( @kontaktUppgift,@CafeId)

    SET @CafeId = SCOPE_IDENTITY();

    COMMIT TRAN
    END TRY

    BEGIN CATCH
    –Ã…terställer till innan begin tran
    ROLLBACK TRAN
    RAISERROR(‘fel i överföringen.’ ,16,1)
    END CATCH
    END

    Reply
  • Alexandre Bizarro
    May 28, 2009 2:09 am

    Tku….
    very smart!

    Reply
  • how to get identity value of last updated record?

    Reply
  • Dave,

    I read recently about a bug which could result in scope_identity() returning the wrong value. Do you know if this issue is still outstanding, and if so, should we avoid scope_identity(), or is it possible to mitigate the problem in some way?

    The issue is described here:

    Reply
  • @@IDENTITY is not working on SQL server 2005.
    Why it is not working?
    thnks.

    Reply
  • Brian Tkatch
    June 29, 2009 8:28 pm

    @Dnyaneshwar

    What is not working about @@IDENTITY? Please give an example of what you are trying, and what you expected.

    Reply
  • Nitin Gautam
    June 30, 2009 2:41 pm

    None of the above is compatible with HSQLDB.
    What if we want to junit the MSSQL DAOs using HSQLDB in memory DB?

    How can we make the query compatible with both SQLServer and HSQLDB??

    Reply
  • I have problem regarding this three

    I have created sp for insert values in to three tables. Those three tables are like steps 1st table’s primary key keeps second table and second table primary key keeps the third table. Please follow following example to more clarification
    insert Table 1 set ID1=SCOPE_IDENTITY()
    Cursor for table 2
    Insert table2 (foreign key ID1 ) SET ID2= SCOPE_IDENTITY()
    Cursor for table 3
    Insert table3 (foreign key ID3 ) SET ID2= SCOPE_IDENTITY()
    END CURESOR 3
    END CURESOR 2

    Can I used Scope_Identity() for this scenario or any other solutions are welcome. Thanks for your advance reply.

    Reply
  • Prateek:
    Do a loop inside every insert. In that way, you’ll be able to retrieve SCOPE_IDENTITY() after each insert

    loop:
    begin
    Insert 1
    loop:
    Insert2
    loop:
    begin
    Insert3
    end
    begin
    end
    end

    Reply
  • Hello All,
    I wish to fetch last record from a table in sql server 2005 without using any group by or stored procedure is it possible

    Reply
  • hello

    would like to know the difference in terminology current session and current scope. Thanks in advance.

    Reply
  • @Sachin Kalra
    If proc A calls B and B calls C – Everything in all the 3 procedures are in the same session.

    However, the SCOPE of a piece of code in PROC C will be limited only to that procedure (C). But it will be in the same session as A and B

    Reply
  • Pinal,

    This is very good, thank you very much

    mathias

    Reply
  • Thanks much, Pinal!!

    Reply

Leave a Reply