The Globally Unique Identifier type of the SQL2000 field ...... Uniqueidentifier

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.