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.

Example

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')

35 comment(S)


shahzeb on Oct 14, 2008 10:35 PM

nice example...

Sachin Gaur on Dec 23, 2008 09:14 PM

That's really nice. But can we get the empty Guid in the SEELCT statement?

Randheer on Jan 20, 2009 11:02 PM

Here the system generates the unique identity itself.
But if I want to generate my own unique identifier using some other columns. Then how can I do it? Please tell the answer.

Name on Feb 18, 2009 07:32 AM

super

Reddymade on Jul 16, 2009 08:57 AM

How can I gernerate a GUID on existing records on a data table?

Alfonso Paredes on Aug 8, 2009 10:11 AM

Reddymade

you can do something like the following

update people
set personId=NEWID()

CM on Sep 24, 2009 03:35 PM

Above didn't work for me so I used a derived table to create an 8 character default password within an existing table.

UPDATE Customers
SET custPword = newPass.newPass
FROM (SELECT Left( NEWID(),8) AS newPass) AS newPass

Pooja on Oct 21, 2009 10:59 AM

That's a good example, compact and perfect.

Rowena on Oct 12, 2010 01:56 AM

Tnx

sponsored

Fayssal El Moufatich on Feb 22, 2011 03:47 AM

For performance reasons and when possible, you might want to use NEWSEQUENTIALID() instead of NEWID()

nidhi on Mar 16, 2011 10:11 PM

Thanks for this eg, its really works for me ...

hich on Jul 8, 2011 09:40 PM

i have to integrate sql server databases from different computers more than 30.
does guid make collision ?
plz help me

hich on Jul 8, 2011 09:40 PM

i have to integrate sql server databases from different computers more than 30.
does guid make collision ?
plz help me

Tiger on Jul 21, 2011 06:34 AM

@Ramesh...

never say never ;-)

GUID on Aug 26, 2011 12:45 AM

@Ramesh : Actually Guid can collide but probability of that is next to impossible :)

they generate somewhere around 6 billion (not sure but a very high number) different combination. So they can collide.

jezzzzz on Sep 25, 2011 11:02 AM

nice! this is all i ever needed! ^_^ thnks!

stuff on Jan 20, 2012 07:17 AM

There are 122 random bits (128 - 2 for variant - 4 for version) so this calculates to 2^122 or 5,316,911,983,139,663,491,615,228,241,121,400,000 possible


I'm not sure where you got your number... 6 billion... this is exponentially more than that. The chance of them colliding is virtually zero across thousands of machines. You are VERY wrong 'GUID'

aisha on Jun 20, 2012 11:15 PM

thanks heaps..

Perumal B on Jun 26, 2012 09:38 AM

Thanks. This is very good.

chaos on Jul 13, 2012 11:22 AM

@stuff -

Whether it's 6 billion or 2^122 or 10^10^100 combinations, there's still a chance of collision... and THAT was GUID's point.

The chance may be nominally zero, but definitely not zero.

Alan on Aug 7, 2012 07:20 AM

Thanks!

TopVisitedSites on Aug 14, 2012 09:59 PM

nice. It's working for me.

Kevin Murphy on Sep 17, 2012 11:18 PM

My question is, if I want to generate the new guid on the basis of the user's last name, first initial and 4 numerical numbers how would this statment be written?

Sagar T on Feb 14, 2013 09:31 AM

How do I generate a GUID without hyphens(-), and specific number of characters ?

ghfhfg on Mar 14, 2013 05:59 AM


Midani on Apr 2, 2013 03:05 PM

Hello mine does not work:

SELECT NEWID()
GO
/******This will return a new random unique identifier, e.g., ******/
/******E75B92A3-3299-4407-A913-C5CA196B3CAB ******/

/******assign uniqueidentifier in a variable ******/

DECLARE @MessageID uniqueidentifier

SET @MessageID = NEWID()


/****** Inserting data in map_Missing_ADT table. ******/

INSERT INTO [DEPTH].[correction].[map_Missing_ADT]
(MapID, TypeID, MessageID, Value, Description)
VALUES
('5806D9F7-D836-456B-92E1-20AFC9F9358A', 'IC', (NEWID(), 'D14', 'XYZ COMPANY')

MANOJ KUMAR on Sep 30, 2013 04:16 AM

i m apply this but i dont know about how guid change in coding part

MANOJ KUMAR on Sep 30, 2013 04:16 AM

i m apply this but i dont know about how guid change in coding part

MANOJ KUMAR on Sep 30, 2013 04:16 AM

i m apply this but i dont know about how guid change in coding part

dsfds on Oct 9, 2013 04:04 AM


SimioCósmico on Nov 20, 2013 09:25 AM

@Midani - You have an extra '(' in your values statement.

BV on Nov 27, 2013 03:02 AM

Nice one. Really appreciate it. Very useful. Thanks.

sonu on Dec 19, 2013 06:10 AM


Eddy on Feb 3, 2014 06:24 PM

NEWID() randomly generates a guaranteed unique value based on the identification number of the server's network card plus a unique number from the CPU clock.
http://www.mssqltips....ntifier-or-identity/

Leave a comment