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)
230 Comments. Leave new
Thanks very much. It really helped me to understand difference between various options of identity.
My problem resolve. God Bless you.
I always have a first choice to search here.
Keep up the good work.
Joggee
Hello sir,
i have 1 doubt. how can i retrieve last 10 records from a particular table.
pls i want answer for this
If you have any unique column
select top 10 * from table
order by unique_col desc
hello sir,
please send me the query of fetch 10 random rows from table,
select top 10 * from table
order by newid()
Good Article
Excellent, it’s very clear and understandable article.
Thanks
Hi,
SELECT IDENT_CURRENT(’tablename’)
The above selects the last record from tablename.
How do i loop through the last 10 records?
Thanks
select top 10 * from tablename
order by identity_col desc
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
Nice and neat.
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
“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
“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
“Ayyappan: Can we have more than one identity column in a table?”
Ayyappan,
There can be only one IDENTITY column per table.
“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.
hey, nice article. thnx!
Penal, with so many people following your blog you should consider putting some advertising on it…
thanks, it helped me to solve a tricky problem
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() ??
You always have the answer I need – thank you!!!!
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