In this article I will explain how to write query to get last inserted record id in sql server or last modified record id value in SQL Server.
Before write query first design one table and give name as EmployeeDetails in your database as shown below
Column Name
|
Data Type
|
Allow Nulls
|
EmpId
|
Int
(set Identity=true)
|
No
|
EmpName
|
varchar(50)
|
Yes
|
Role
|
Varchar(50)
|
Yes
|
INSERT INTO
EmployeeDetails(EmpName,Role) VALUES('SureshDasari','Team Lead')
|
SELECT @@IDENTITY
SELECT SCOPE_IDENTITY()
SELECT IDENT_CURRENT('TableName')
|
SELECT MAX(EmpId) FROM EmployeeDetails
|
SELECT TOP 1 EmpId FROM EmployeeDetails ORDER
BY EmpId DESC
|
|
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
Subscribe by RSS
Subscribe by Email
|
|||
|
|
|
Subscribe by RSS
Subscribe by Email
3 comments :
I am fan of ur posts...
tell me what if Identity=false?
if identity is false you need to increment id by yourself so as not to conflict with previous id and use SELECT TOP 1 EmpId FROM EmployeeDetails ORDER BY EmpId DESC
how to write both queries in single line i mean how to get identity id after inserting a row in single query...l