Skip to main content

Difference between @@Identity, Scope_Identity (), IDENT_Current(‘tablename’)

Problem: Which one of the following should be use to get the last Identity of the recently added row in T-SQL or Stored Procedure: @@Identity, Scope_Identity (), IDENT_Current(‘tablename’)
Solution: So many times we required to get the last inserted rows identity to be insert them in child table for reference in stored procedure. All the above statements gives us the last identity inserted but in different perspective.
So if you want to get the last identity in T-SQL or Stored Procedure always use Scope_Identity () to avoid problems in Multiuser (concurrent) scenarios. Let me explain why?
Lets see what does each means?
@@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.
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.
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.

Comments

Popular posts from this blog

Custom Effective Paging With Sorting across all pages GridView in Asp.Net

Hi, I need to comeback to blogging anyhow, since it was too long break. Hence yesterday  i decided to start blogging again, and thought of this topic.  When Microsoft shipped GridView control with Asp.net, it came with lot of new functionality, but it also lacking of existing functionality  like 'VirtualCount' property  which was available with DataGrid but not with GridView. Since because of this it is very much difficult to implement Custom Paging with Grid View.  Now what is custom paging and default paging?  To answer this question i will take an example. I need to display records on a page in tabular format, with paging and sorting.  Well if you go with default paging and default sorting your development work is easy. But with it there is a drawback. - It is grid view control which decides what records to display and sorted by which column. So for that it need complete set of all records. Suppose if you want to display only 25 records, a...

Sql Server Tips and Guidelines that Developer Must Know

Introduction The following Tips and Guidelines are very important from the performance perspective of your SQL Server development or production database. The best way to achieve optimum performance benefits is to experiment with the following guidelines while using/viewing a query execution plan, both with and without implementing the respective tip/guideline. Tips and Guidelines • As a common practice, every table should have a clustered index. Generally, but not always, the clustered index should be on a column that monotonically increases, such as an identity column or some other column where the value is unique. In many cases, the primary key is the ideal column for a clustered index. • Indexes should be measured on all columns that are frequently used in WHERE , ORDER BY , GROUP BY , TOP and DISTINCT clauses. • Do not automatically add indexes on a table because it seems like the right thing to do. Only add indexes if you know that they will be used by the querie...

Learn How to Extend Visual Studio

Millions of developers in both small and large enterprises use Visual Studio to more easily use the latest platform technology to revolutionize the way they build, maintain, and deploy today’s complex business applications. Visual Studio supports a rich, diverse ecosystem of developers who ship thousands of Visual Studio integrated products to make it easy for Visual Studio users to use their favorite tool their way. And if someone else hasn’t created it, you can -- the Visual Studio SDK is freely available to the developer community and contains everything you need to extend Visual Studio. With Visual Studio, you’ll find new opportunities to build extensions for specific development needs. These include the Visual Studio Shell, a foundational IDE for custom tools and programming languages, Domain-Specific Language (DSL) tools, a set of tools to help you create graphical models that generate code, and support for Visual Basic within the Visual Studio SDK. Released on a quarterly basis,...