Get Newly Inserted raw Id in SQL

Hai All,

             While Programming with the SQL we may face situations to get the newly inserted raw Id from the query. Here i am showing the example code to achieve the same.




INSERT INTO tablename(Name) VALUES (@Name)

SET @res = (SELECT SCOPE_IDENTITY())


here the "SCOPE_IDENTITY()" will return the row id of the just inserted raw of that instance.


In Some Situation we may face to use "@@IDENTITY"



INSERT INTO tablename(Name) VALUES (@Name)

SET @res = (SELECT @@IDENTITY)


it differs the value while using the triggers for inserting, if we use triggers, "@@ IDENTITY" will return the trigger value identity.


Thanks














SHARE

Gibin Francis

Hi. I’m Software Engineer in India. I’m Web Developer, Creative Coder, Web Designer, logo Designer, Business Enthusiast, StartUp Enthusiast, Speaker, Writer and Designer. Inspired to make things looks better and Do the this in the best way.

    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment