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

get last inserted primary key

INSERT INTO dbo.Table(columns)
OUTPUT INSERTED.p_key, INSERTED.someothercolumnhere .......
VALUES(...) 
Comment

PREVIOUS NEXT
Code Example
Sql :: SQL Rename Column in a Table 
Sql :: delete * from where id = 1; 
Sql :: t sql dynamic top n query 
Sql :: mysql if statement 
Sql :: sql server today minus n 
Sql :: truncate table in sql 
Sql :: docker add mysql to image 
Sql :: postgresql concat string with separator 
Sql :: sql where contains part of string 
Sql :: run docker container with database as rds metabase 
Sql :: add column alter table default value 
Sql :: sqlite 3 mac 
Sql :: sql restore database from backup 
Sql :: postgresql could not start server mac 
Sql :: tsql pad left 
Sql :: sqlite get date only 
Sql :: new uniqueidentifier in sql 
Sql :: porcentaje sql 
Sql :: sql vs nosql or mysql vs mongodb 
Sql :: postgres date 
Sql :: how to convert external table to managed table in hive 
Sql :: ImportError: DLL load failed while importing _sqlite3: The specified module could not be found. 
Sql :: insert into postgres 
Sql :: double in sql server example 
Sql :: between date in sql server 
Sql :: sql describe 
Sql :: oracle sql developer 
Sql :: sql server find all referencing objects to user-defined table type 
Sql :: how to select from mssql 
Sql :: show database not empty tables postgres 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =