insert into sql id code example
Example 1: sql insert inserted id
-- Never use @@identity or scope_identity()
-- they can not always be relied upon
DECLARE @Ids_tbl TABLE ([id] INT); -- Requires table. use variable for scope
INSERT INTO [sometable] ([ColA],[ColB])
OUTPUT INSERTED.ID INTO @Ids_tbl(id)
VALUES ('valA','valB');
SELECT [id] FROM @Ids_tbl; -- <-- Id(s) in here
Example 2: id increment ms sql server
CREATE TABLE Persons (
Personid int IDENTITY(1,1) PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int
);