Uniqueidentifier
Declare @ myid uniqueidentifier
Set @ myid = newid ()
Print 'value of @ myid is: '+ convert (varchar (255), @ myid)
The following is the result set:
CopyCode
Value of @ myid is: 6f9619ff-8b86-d011-b42d-00c04fc964ff
Note:
Newid returns different values for each computer. The displayed number only serves as an explanation.
B. Use newid In the CREATE TABLE statement
The following example creates a Cust table with the Data Type uniqueidentifier and uses newid to fill the table with the default value. When newid () is assigned a default value, each new row and existing row have a unique value for the customerid column.
-- creating a table using newid for uniqueidentifier data type.
Create Table Cust
(
customerid uniqueidentifier not null
default newid (),
company varchar (30) not null,
contactname varchar (60) not null,
address varchar (30) not null,
city varchar (30) not null,
stateprovince varchar (10) null,
postalcode varchar (10) Not null,
countryregion varchar (20) not null,
telephone varchar (15) not null,
Fax varchar (15) null
)
go
-- inserting data into Cust table.
insert Cust
(customerid, company, contactname, address, city, stateprovince,
postalcode, countryregion, telephone, fax)
values
(newid (), 'wartian herkku ', 'pirkko koskitalo', 'torikatu 38 ', 'ouulu', null,
'123 ', 'finland', '2014-981 ', '2014-443655')
Insert cust
(Customerid, company, contactname, address, city, stateprovince,
Postalcode, countryregion, telephone, fax)
Values
(Newid (), 'wellington Importadora ', 'paula parente', 'rua do Mercado, 12', 'resende', 'SP ',
'2014-08737 ', 'brasil', '(14) 555-8122 ','')
C. assign values using uniqueidentifier and variables
The following example declares a local variable named @ myid as a uniqueidentifier data type variable. Then, assign values to the variable using the set statement.
Copy code
Declare @ myid uniqueidentifier
Set @ myid = 'a972c577-DFB0-064E-1189-0154C99310DAAC12'
Go
========================================================== ========================================================== ==========
The uniqueidentifier data type can store 16-byte binary values, which act the same as the Globally Unique Identifier (guid. GUID is the unique binary number. No duplicate guid value is generated on any two computers in the world. GUID is used to assign a unique identifier to a network with multiple nodes and computers.
The guid value of the uniqueidentifier column is usually obtained in one of the following ways:
Call the newid function in a Transact-SQL statement, batch processing, or script.