What is Scope_Identity() & Ident_Current() in Sql Server


SCOPE_IDENTITY(): It is useful when we want to know that during inserting of a record what is the current ID value of a row. Lets say we are inserting a record in Student table which contains a column StudentId which is auto generated. So, in this scenario if we want to know what will be the StudentId we can get it by using scope_identity(). It will return the last identity value created in the current session.

Query:
select scope_identity() //execute this after inserting the record.

IDENT_CURRENT(): 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.
  • It is not limited by scope and session, it is limited to a specified table.
  • It returns the identity value generated for a specific table in any session and any scope.

If we want to know the last inserted record in any table regardless of the scope then, execute following:
 
Query:
select IDENT_CURRENT('Student')as CurrentIdInserted

Note: scope_identity() or IDENT_CURRENT('tablename')will work only if the table has a column set with the property Identity Specification as Yes. e.g. RecordId

 

Comments

  1. Dot Net is an ever trending technology where it is more preferable by developers to utilize the features in the dot net language. Your article on Dot Net language proves that it is an evergreen technology in the IT market.
    Regards:
    .asp.net training in chennai
    .net training

    ReplyDelete
  2. This blog is really good I would like to say thanks please share more content on msbi online training

    ReplyDelete
  3. Thanks for share this information. I have read your blog. Your information
    is really helpful for me. Keep update your blog.
    redbeardpress
    Education

    ReplyDelete
  4. Thanks for any other wonderful post. Where else may just anyone get that type of info in such a perfect means of writing? I’ve a presentation next week, and I am on the look for such information.

    hadoop training in chennai

    hadoop training in omr

    salesforce training in chennai

    salesforce training in omr

    c and c plus plus course in chennai

    c and c plus plus course in omr

    machine learning training in chennai

    machine learning training in omr

    ReplyDelete
  5. Excellent Blog! I would like to thank for the efforts you have made in writing this post.
    I am hoping the same best work from you in the future as well. I wanted to thank you for this websites!
    java training in chennai

    java training in tambaram

    aws training in chennai

    aws training in tambaram

    python training in chennai

    python training in tambaram

    selenium training in chennai

    selenium training in tambaram

    ReplyDelete

Post a Comment

Popular posts from this blog

How to start a free blog with Blogger in simple steps

How to verify/add a blogger blog in Google Webmaster Tool or Search Console. How to verify your site's ownership.

Structs in C#