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

  • Thanks very much. It really helped me to understand difference between various options of identity.

    Reply
  • My problem resolve. God Bless you.
    I always have a first choice to search here.

    Keep up the good work.

    Joggee

    Reply
  • Hello sir,

    i have 1 doubt. how can i retrieve last 10 records from a particular table.

    pls i want answer for this

    Reply
  • hello sir,
    please send me the query of fetch 10 random rows from table,

    Reply
  • Good Article

    Reply
  • Excellent, it’s very clear and understandable article.

    Thanks

    Reply
  • Hi,

    SELECT IDENT_CURRENT(’tablename’)

    The above selects the last record from tablename.
    How do i loop through the last 10 records?

    Thanks

    Reply
  • hi, i get last id of inserted data by using SCOPE_IDENTITY() from table1

    Now my problem is how to insert the id from SCOPE_IDENTITY() to table 2

    any guide are welcome

    Thanks in advance

    Reply
  • Nice and neat.

    Reply
  • Jacob Sebastian
    January 29, 2009 4:34 pm

    sha,

    A safe way to do is to assign the SCOPE_IDENTITY value to a variable after the first INSERT statement. For example:

    DECLARE @id INT
    INSERT INTO firstTable(col1) SELECT 1
    SELECT @id = SCOPE_IDENTITY()

    INSERT INTO secondTable(col1, id) SELECT 1,@id

    Reply
  • Jacob Sebastian
    January 29, 2009 4:38 pm

    “Helvin

    Hi,

    SELECT IDENT_CURRENT(’tablename’)

    The above selects the last record from tablename.
    How do i loop through the last 10 records?

    Thanks”

    Helvin,
    Probably the easiest option is to select the last 10 records. For example:

    SELECT TOP 10 col1, col2, colN
    FROM tablename
    ORDER BY IdentityColumn

    Reply
  • Jacob Sebastian
    January 29, 2009 4:40 pm

    “Bharathidasan : how can i retrieve last 10 records from a particular table.”

    Bharathidasan,
    Try this:
    SELECT TOP 10 col1, col2, colN
    FROM tablename
    ORDER BY IdentityColumn

    Reply
  • Jacob Sebastian
    January 29, 2009 4:41 pm

    “Ayyappan: Can we have more than one identity column in a table?”

    Ayyappan,
    There can be only one IDENTITY column per table.

    Reply
  • Jacob Sebastian
    January 29, 2009 4:42 pm

    “suresh: how can i add identity coloumn to existing coloum through query”

    There is no direct way to do this. However, I recently found a script that helps to do this.

    Reply
  • hey, nice article. thnx!

    Reply
  • Penal, with so many people following your blog you should consider putting some advertising on it…

    Reply
  • thanks, it helped me to solve a tricky problem

    Reply
  • thanks, nice one.
    I am doing T-SQL perfomance analysis. In my case I have no issue in using eigther of this, Can anyone tell me that which one faster @@identity or SCOPE_IDENTITY() ??

    Reply
  • Christopher Haigood
    February 20, 2009 3:17 am

    You always have the answer I need – thank you!!!!

    Reply
  • harinath clavib
    March 11, 2009 3:43 am

    Hi pinale
    i’m unable to access db it display’s timedout !

    is any problem with deadlock or network as i checked by using trace files but i could not find any error msg on deadlocks.

    give me your valuabl sugesstion.

    Regards,
    Harinath

    Reply

Leave a Reply