Getting the last insert

You will find below, an easy way to get the last inserted value after the database has generated a UNIQUEIDENTIFIER. In that way you have no excuse to generate an UNIQUEIDENTIFIER code side and force the default value in the table…
First of all we create a table. For each insertion, a new sequential ID will be automatically generated by the database.

CREATE TABLE [dbo].[TESTTABLE_NEWSEQUENTIALID](
      [ID] [uniqueidentifier] DEFAULT (newsequentialid()) NOT NULL PRIMARY KEY CLUSTERED,
      [Value] INT,
)

We can now easily get the last Guid generated by an insertion

DECLARE @tGuid TABLE (lastGuid uniqueidentifier)

INSERT INTO dbo.TESTTABLE_NEWSEQUENTIALID (SEQUENCE)
OUTPUT INSERTED.ID INTO @tGuid
VALUES (15)

SELECT lastGuid FROM @tGuid