Generates human-readable, globally unique IDs (GUIDs) in two different formats, based on arguments you provide. The length of the human-readable format of the GUID value is either 32 bytes (with no dashes) or 36 bytes (with dashes).
newid([optionflag])
0, or no value – the GUID generated is human-readable, but does not include dashes. This argument, which is the default, is useful for converting values into varbinary.
-1 – the GUID generated is human-readable and includes dashes.
-0x0 – returns the GUID as a varbinary.
Creates a table with varchar columns 32 bytes long and then uses newid with no arguments with the insert statement.
create table t (UUID varchar(32)) go insert into t values (newid()) insert into t values (newid()) go select * from t
UUID -------------------------------- f81d4fae7dec11d0a76500a0c91e6bf6 7cd5b7769df75cefe040800208254639
Produces a GUID that includes dashes.
select newid(1) go
------------------------------------ b59462af-a55b-469d-a79f-1d6c3c1e19e3
Creates a default that converts the GUID format without dashes to a varbinary(16) column:
create table t (UUID_VC varchar(32), UUID varbinary(16)) go create default default_guid as strtobin(newid()) go sp_bindefault default_guid, "t.UUID" go insert t (UUID_VC) values (newid()) go
newid generates two values for the globally unique ID (GUID) based on arguments you pass to newid. The default argument generates GUIDs without dashes. Any other value passed to newid generates GUIDs with dashes and is more easily readable.
newid can be used in defaults, rules, and triggers, similar to other functions.
Make sure the length of the varchar column is at least 32 bytes for the GUID format without dashes, and at least 36 bytes for the GUID format with dashes. The column length is truncated if it is not declared with these minimum required lengths. Truncation increases the probability of duplicate values.
An argument of zero is equivalent to the default.
You can use the GUID format without dashes with the strtobin function to convert the GUID value to 16-byte binary data. However, using strtobin with the GUID format with dashes results in NULL values.
Because GUIDs are globally unique, they can be transported across domains without generating duplicates.
ANSI SQL – Compliance level: Transact-SQL extension.
Any user can execute newid.