NEWID (Transact-SQL)
Returns a unique uniqueidentifier value that is guaranteed to be different from any other uniqueidentifier value that has been generated by any other instance of SQL Server or by any other Transact-SQL statement.
Syntax
NEWID ( )
Return Value
Returns a uniqueidentifier value.
Description
NEWID generates a random GUID (Globally Unique Identifier). The value is generated by using the network adapter's MAC address and the current system time, or by using a random number sequence if the network adapter's MAC address is not available.
Note: If you need to generate a uniqueidentifier value that is based on a specific algorithm or a seed value, consider using the
NEWSEQUENTIALID function. NEWSEQUENTIALID generates GUIDs that are guaranteed to be unique and sequentially greater than any previously generated GUID on the same computer. This can improve performance for index inserts.
Examples
A. Generating a uniqueidentifier value
The following example shows how to generate a uniqueidentifier value and assign it to a variable:
DECLARE @MyNewGuid uniqueidentifier SET @MyNewGuid = NEWID() SELECT @MyNewGuid AS 'Generated GUID'
B. Inserting a uniqueidentifier value into a table
The following example shows how to insert a uniqueidentifier value into a table column:
CREATE TABLE MyTable ( ID uniqueidentifier PRIMARY KEY , SomeValue VARCHAR(50) ) INSERT INTO MyTable (ID, SomeValue) VALUES (NEWID(), 'Example Data') SELECT ID, SomeValue FROM MyTable