Insert with output

Posted by Joe Enos on August 11, 2021 · 1 min read

This can be an alternative to things like SCOPE_IDENTITY() or @@IDENTITY.

declare @result table (ID int);

-- dbo.Records has identity column ID
insert dbo.Records (PersonName)
output inserted.ID into @result
values (@PersonName);

select ID from @result; -- The newly created identity