How to find the last identity value inserted in the SQL Server
Posted on 1/10/2008 9:01:56 AM
in #SQL SERVER
Hi,/SPAN>/P>
When inserting a row in the database with an identity column as a primary key, most of the time we need to capture the new identity value generated. In SQL Server there cane as many as three approaches for the same. /SPAN>/P>
/SPAN>@@IDENTITY/SPAN>/P>
/SPAN>SCOPE_IDENTITY()/SPAN>/P>
/SPAN>IDENT_CURRENT(‘tablename’)/SPAN>/P>
All of them can be used to find the last identity value inserted in the database, but they differ in the functionality depending on the scope or source of the insert as well as the connection that insert the row./SPAN>/P>
The server variable @@IDENTITY will return the last generated identity value accross all scope but for the same connection. The value returned will be for the last table inserted with identity column in the same connection. This means that if we insert some record in table (TableA) which has a trigger on the insert and the trigger inserts a record in some other table (TableB) with identity column then the @@IDENTITY will return the identity value inserted in TableB./SPAN>/P>
Function SCOPE_IDENTITY() is identical to @@IDENTITY with one exception. The value returned is limited to the current scope (i.e. the executed stored procedure). So in our previous example the value returned will the identity value inserted in TableA./SPAN>/P>
Finally, function IDENT_CURRENT spans all scope and all connections to retrieve the last generated table identity value. But the function is table specific and returns the value for the given table only./SPAN>/P>
Vikram/SPAN>/P>
|
Posted on 12/30/2008 3:56:01 PM
This article has been a big help. Thanks!
/p>
|
Posted on 7/16/2009 9:38:21 AM
Hello All,
/p>
NameSpace is logical group of classies which help to develop an application.
/p>
Namespace allows you to use in-built .NET Classes. "System" at top(begining) of the code behind page.
/p>
In C#, We have to write "using" keyword begining of namespace e.g "using System".
/p>
In VB, We have to write "Imports" keyword begining of namespace e.g "Imports System".
/p>
System: Root for All other namespace in Dot Net. It is basic namespace for all other namespace.
/p>
I have put article about namespace in DotNet.. Must read.. It's really helpfull
/p>
for further infomration open "http:////developmentwithdotnet.blogspot.com/a>"
/p>
Thanks
-Nimesh.
/p>
|