Search
 
SCRIPT & CODE EXAMPLE
 

SQL

sql get last primary key inserted

-- @@IDENTITY - Returns the last identity created in the same session
-- SCOPE_IDENTITY() - Returns the last identity created in the same session and the same scope
-- IDENT_CURRENT() - Returns the last identity created for a specific table or view in any session
-- OUTPUT Inserted.[ColumnName] INTO [Table] - Returns a list of created identities (used for multi inserts)

/*Examples Start*/
IF OBJECT_ID('tempdb..#tmpT') IS NOT NULL
	DROP TABLE #tmpT

IF OBJECT_ID('TableA') IS NOT NULL
	DROP TABLE TableA

CREATE TABLE TableA (
	ID NUMERIC(18,0) IDENTITY(1,1),
	Name VARCHAR(255),
	PRIMARY KEY (ID)
)

INSERT INTO TableA (Name) VALUES ('Jane')
SELECT @@IDENTITY AS [ID]

INSERT INTO TableA (Name) VALUES ('John')
SELECT SCOPE_IDENTITY() AS [ID]

INSERT INTO TableA (Name) VALUES ('Smith')
SELECT IDENT_CURRENT('dbo.TableA') AS [ID]

DECLARE @NewIds TABLE(ID NUMERIC(18,0))
INSERT INTO TableA (Name)
OUTPUT Inserted.ID INTO @NewIds
VALUES ('Joe'), ('Bloggs')
SELECT * FROM @NewIds

CREATE TABLE #tmpT (ID NUMERIC(18,0))
INSERT INTO TableA (Name)
OUTPUT Inserted.ID INTO #tmpT
VALUES ('Joe'), ('Bloggs')
SELECT * FROM #tmpT
/*Examples End*/
Comment

get primary key of last inserted record sql server

CREATE TABLE #a(identity_column INT IDENTITY(1,1), x CHAR(1));
INSERT #a(x) VALUES('a');

SELECT SCOPE_IDENTITY();

-- 4 ways to get identity IDs of inserted rows in SQL Server
INSERT INTO TableA (...) VALUES (...)
SELECT @@IDENTITY

INSERT INTO TableA (...) VALUES (...)
SELECT SCOPE_IDENTITY()

SELECT IDENT_CURRENT('dbo.TableA')

DECLARE @NewIds TABLE(ID INT, ...)
INSERT INTO TableA (...)
OUTPUT Inserted.ID, ... INTO @NewIds
SELECT ...
Comment

How to get last inserted primary key in SQL Server

-- 4 ways to get identity IDs of inserted rows in SQL Server

INSERT INTO TableA (...) VALUES (...)
SET @LASTID = @@IDENTITY

INSERT INTO TableA (...) VALUES (...)
SET @LASTID = SCOPE_IDENTITY()

SET @LASTID = IDENT_CURRENT('dbo.TableA')

DECLARE @NewIds TABLE(ID INT, ...)

INSERT INTO TableA (...)
OUTPUT Inserted.ID, ... INTO @NewIds
SELECT ...
Comment

PREVIOUS NEXT
Code Example
Sql :: mysql get first n characters of string 
Sql :: launch sql script from docker in mysql 
Sql :: mysql regexp match word 
Sql :: dual in db2 
Sql :: activate log mariadb 
Sql :: alter column set not null to null postgres 
Sql :: with postgres 
Sql :: mysql delete entire row on condition 
Sql :: oracle table free space 
Sql :: ERROR 3948 (42000): Loading local data is disabled; this must be enabled on both the client and server sides 
Sql :: mysql remove auto increment 
Sql :: Write an SQL query to print details of the Workers who have joined in Feb’2014 
Sql :: sql set 
Sql :: query distinct 
Sql :: postgres top 10 
Sql :: oracle convert run duration to number 
Sql :: update sqlite 
Sql :: mysql function variable 
Sql :: pgsql is not permitted to log in 
Sql :: sql if example 
Sql :: charindex 
Sql :: alter or change postgresql sequence counter 
Sql :: how to write lowercase in sql 
Sql :: oracle concat datetime 
Sql :: mysql not equal 
Sql :: declare value in sql 
Sql :: sql primary key syntax 
Sql :: sql add column with default value 
Sql :: select into 
Sql :: update multiple columns in sql 
ADD CONTENT
Topic
Content
Source link
Name
9+9 =