Insert with output in SQL Server

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

Leave a Reply

Your email address will not be published.