Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs

SQL @@IDENTITY

by suresh

The SQL @@IDENTITY is a System Function that returns the last inserted identity value. You can use this SQL Server @@IDENTITY after an INSERT, INSERT INTO SELECT, BULK INSERT, or SELECT INTO Statement is completed to find the last generated identity value. And, if there is no insert operation happened, it will return NULL.

How to write or use this @@IDENTITY in SQL Query to extract identity value with example?. For this @@IDENTITY demonstration, we are going to use the below-shown table data.

SQL @@IDENTITY 0

We are creating the following SQL table to insert the data into it.

USE [SQL Tutorial]
GO

CREATE TABLE [dbo].[EmployeeDuplicates](
	[EmpID] [int] IDENTITY(1,1) NOT NULL,
	[FirstName] [nvarchar](255) NULL,
	[LastName] [nvarchar](255) NULL,
	[Education] [nvarchar](255) NULL,
	[Occupation] [nvarchar](255) NULL,
	[YearlyIncome] [float] NULL,
	[Sales] [float] NULL,
	[HireDate] [datetime] NULL,
 CONSTRAINT [PK_EmployeeDuplicates_EmpID] PRIMARY KEY CLUSTERED 
 (
	[EmpID] ASC
 )
)

SQL @@IDENTITY Example 1

In this example first, we will insert four random records into the Employee Duplicates table. Next, we will extract the last inserted identity value using the @@IDENTITY.

Let me insert value into the table using the INSERT Statement.

-- Example for SQL Server IDENTITY 
USE [SQL Tutorial]
GO

INSERT INTO [dbo].[EmployeeDuplicates]
	  ([FirstName], [LastName], [Education], [Occupation], [YearlyIncome], [Sales], [HireDate])
     VALUES
           ('Tutorial', 'Gateway', 'Masters','Admin', 92500, 3200, '2006-01-28 13:10:02.047'),
	   ('Steve', 'Lara', 'Graduate', 'Software Developer', 19500, 1200, '2012-04-28 13:10:02.047'),
	   ('Jack', 'Smith', 'Graduate', 'Manager', 25000, 150.45, '2015-05-26 13:12:02.047' ),
	   ('Ramesh', 'Kumar', 'Post Graduate', 'Sales Manager', 75000, 240.89, '2016-04-18 13:10:02.047')
GO

SELECT @@IDENTITY AS [Last Identity Value]
GO

SELECT MAX(EmpID) AS [Maximum Identity Value]
FROM [EmployeeDuplicates]
GO
SQL @@IDENTITY 1

Let me show you the data that we inserted into the Employee Duplicates tables

SQL @@IDENTITY 2

@@IDENTITY Example 2

In this example, first, we will use the INSERT, INSERT INTO SELECT to insert records from the Employee table into the Employee Duplicates table. Next, we will extract the last inserted identity value in SQL Server using the @@IDENTITY

-- Example for SQL Server IDENTITY 
USE [SQL Tutorial]
GO
INSERT INTO [EmployeeDuplicates] (
[FirstName], [LastName], [Education], [Occupation], [YearlyIncome], [Sales], [HireDate])
   SELECT [FirstName], [LastName], [Education], [Occupation], [YearlyIncome], [Sales], [HireDate]
   FROM [Employee] AS Emp
   WHERE Emp.EmpID > 4
GO

SELECT @@IDENTITY AS [Last Identity Value]
GO

SELECT MAX(EmpID) AS [Maximum Identity Value]
FROM [EmployeeDuplicates]
GO
SQL @@IDENTITY 3

Let me show you the data that we inserted into the Employee Duplicates tables

SQL @@IDENTITY 4

SQL @@IDENTITY Example 3

In this example first, we will show you what will happen if the Insert statement fails for some reason. To demonstrate this @@IDENTITY, we are deliberately failing the insert operation.

-- Example for SQL Server IDENTITY 
USE [SQL Tutorial]
GO

INSERT INTO [dbo].[EmployeeDuplicates]
	  ([FirstName], [LastName], [Education], [Occupation], [YearlyIncome], [Sales], [HireDate])
     VALUES
           ('Tutorial Gateway', 'Website', 'Programming','Student', 2000,  '2006-01-28 13:10:02.047')
GO

SELECT @@IDENTITY AS [Last Identity Value]
GO

SELECT MAX(EmpID) AS [Maximum Identity Value]
FROM [EmployeeDuplicates]
GO
SQL @@IDENTITY 5

Let me show you the data that we inserted into the Employee Duplicates tables

SQL @@IDENTITY 6

As you can see, @@IDENTITY (last identity value) is returning a NULL value.

Placed Under: SQL

  • Install SQL Server
  • Install SQL Management Studio
  • Uninstall Management Studio
  • Install AdventureWorks Database
  • SQL Management Studio Intro
  • Connect SQL with sqlcmd utility
  • SQL Attach Database
  • SQL Detach Database
  • SQL Restore Database
  • Restore Database using BAK
  • SQL Rename Database with Files
  • Get SQL Database Names
  • SQL Create Table
  • SQL Rename Table
  • SQL Alter Table
  • SQL Add Column
  • SQL Rename Column
  • Get SQL Table Names in a DB
  • Find SQL Table Dependencies
  • Rename SQL Table & Column
  • SQL Global & Local Temp Table
  • SQL Table Variable
  • SQL Derived Table
  • SQL DATALENGTH
  • SQL Data Types
  • DML, DDL, DCL & TCL Cmds
  • SQL Query Builder
  • SQL ALIAS
  • SQL SELECT Statement
  • SQL SELECT DISTINCT
  • SQL SELECT INTO Statement
  • SQL INSERT Statement
  • SQL INSERT INTO SELECT
  • SQL BULK INSERT or BCP
  • SQL UPDATE Statement
  • SQL UPDATE from SELECT
  • SQL DELETE Statement
  • SQL TRUNCATE Table
  • SQL CASE Statement
  • SQL MERGE Statement
  • SQL Subquery
  • SQL CTE
  • SQL PIVOT
  • SQL UNPIVOT
  • SQL Clauses Examples
  • SQL TOP Clause
  • SQL WHERE Clause
  • SQL ORDER BY Clause
  • SQL GROUP BY Clause
  • SQL HAVING Clause
  • SQL Primary Key
  • SQL Foreign Key
  • SQL Referential Integrity
  • SQL Check Constraint
  • SQL Unique Constraint
  • SQL Default Constraint
  • SQL Clustered Index
  • SQL Non Clustered Index
  • SQL Filtered Indexes
  • SQL COALESCE Function
  • SQL IS NOT NULL
  • SQL IS NULL Function
  • SQL ISNULL
  • SQL JOINS
  • SQL CROSS JOIN
  • SQL FULL JOIN
  • SQL SELF JOIN
  • SQL Outer Joins
  • SQL Cross Join Vs Inner Join
  • SQL LEFT JOIN
  • SQL RIGHT JOIN
  • SQL AND & OR Operators
  • SQL Arithmetic Operators
  • SQL BETWEEN Operator
  • SQL Comparison Operators
  • SQL LIKE
  • SQL EXCEPT
  • SQL EXISTS Operator
  • SQL NOT EXISTS Operator
  • SQL INTERSECT
  • SQL IN Operator
  • SQL NOT IN Operator
  • SQL UNION
  • SQL UNION ALL
  • SQL IF ELSE
  • SQL ELSE IF
  • SQL WHILE LOOP
  • SQL Nested While Loop
  • SQL BREAK Statement
  • SQL CONTINUE Statement
  • SQL GOTO Statement
  • SQL IIF Function
  • SQL CHOOSE Function
  • SQL Change Data Capture
  • SQL Table Partitioning
  • SQL Table Partition using SSMS
  • SQL TRY CATCH
  • SQL VIEWS
  • SQL User Defined Functions
  • SQL Alter User Defined Functions
  • SQL Stored Procedure Intro
  • Useful System Stored Procedures
  • SQL SELECT Stored Procedure
  • SQL INSERT Stored Procedure
  • SQL UPDATE Stored Procedure
  • Stored Procedure Return Values
  • Stored Procedure Output Params
  • Stored Procedure Input Params
  • Insert SP result into Temp Table
  • SQL Triggers Introduction
  • SQL AFTER INSERT Triggers
  • SQL AFTER UPDATE Triggers
  • SQL AFTER DELETE Triggers
  • SQL INSTEAD OF INSERT
  • SQL INSTEAD OF UPDATE
  • SQL INSTEAD OF DELETE
  • SQL STATIC CURSOR
  • SQL DYNAMIC CURSOR
  • SQL FORWARD_ONLY Cursor
  • SQL FAST_FORWARD CURSOR
  • SQL KEYSET CURSOR
  • SQL TRANSACTIONS
  • SQL Nested Transactions
  • SQL ACID Properties
  • Create SQL Windows Login
  • Create SQL Server Login
  • SQL Server Login Error
  • Create SQL Server Roles
  • SQL Maintenance Plan
  • Backup SQL Database
  • SQL Ranking Functions Intro
  • SQL RANK Function
  • SQL PERCENT_RANK Function
  • SQL DENSE_RANK Function
  • SQL NTILE Function
  • SQL ROW_NUMBER
  • SQL Aggregate Functions
  • SQL Date Functions
  • SQL Mathematical Functions
  • SQL String Functions
  • SQL CAST Function
  • SQL TRY CAST
  • SQL CONVERT
  • SQL TRY CONVERT
  • SQL PARSE Function
  • SQL TRY_PARSE Function
  • SQL Calculate Running Total
  • SQL Find Nth Highest Salary
  • SQL Reverse String
  • SQL FOR XML PATH
  • SQL FOR XML AUTO
  • SQL FOR XML RAW

Copyright © 2021 · All Rights Reserved by Suresh

About Us | Contact Us | Privacy Policy