Obtenez la dernière clé primaire insérée
INSERT INTO dbo.Table(columns)
OUTPUT INSERTED.p_key, INSERTED.someothercolumnhere .......
VALUES(...)
Sal-versij
INSERT INTO dbo.Table(columns)
OUTPUT INSERTED.p_key, INSERTED.someothercolumnhere .......
VALUES(...)
-- 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 ...
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 ...