Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR SQL

sql get inserted primary key

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: #sql #inserted #primary #key
ADD COMMENT
Topic
Name
6+2 =