Monday 13 March 2017

IDENTITY in sql server

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 our current session. While @@IDENTITY is limited to the current session, it is not limited to the current scope. If we have a trigger on a table that causes an identity to be created in another table, we will get the identity that was created last, even if it was the trigger that created it.
Let’s see how to create identity
create table product
(
 productID int identity(1,1),
 productName varchar(50),
 cost int
 )


Now I am inserting some records
 insert into product values ('Pen',50)
 insert into product values ('Book',50)
 insert into product values ('Note Book',50)
 insert into product values ('Copy',50)


Product ID is auto incremented
select * from product
select @@IDENTITY As 'Current ProductID'



If I insert a record the value of @@IDENTITY will be change. I have inserted 2 more records see the result.




No comments:

Post a Comment

If you have any doubt, please let me know.

Popular Posts