Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR SQL

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 ...
Source by www.munisso.com #
 
PREVIOUS NEXT
Tagged: #primary #key #inserted #record #sql #server
ADD COMMENT
Topic
Name
9+1 =