Skip to content

Instantly share code, notes, and snippets.

@akkidas
Created April 13, 2016 17:59
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save akkidas/8c1afa5675fdf9abe115eb2cdc47fee8 to your computer and use it in GitHub Desktop.
Save akkidas/8c1afa5675fdf9abe115eb2cdc47fee8 to your computer and use it in GitHub Desktop.
Generate New Guid (uniqueidentifier) in SQL Server
-- If you want to generate a new Guid (uniqueidentifier) in SQL server the you can simply use the NEWID() function.
SELECT NEWID()
GO
-- This will return a new random uniqueidentifier e.g.
E75B92A3-3299-4407-A913-C5CA196B3CAB
To select this Guid in in a variable
--assign uniqueidentifier in a variable
DECLARE @EmployeeID uniqueidentifier
SET @EmployeeID = NEWID()
You can directly use this with INSERT statement to insert new row in table.
-- Inserting data in Employees table.
INSERT INTO Employees
(EmployeeID, Name, Phone)
VALUES
(NEWID(), 'John Kris', '99-99999')
@Vikas-jk
Copy link

Thanks for query, if anyone stumbles upon this and want to learn basics of Guid check
Uniqueidentifier in SQL Server

@leslierhs
Copy link

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment