Newid () creates a unique value of the uniqueidentifier type

Source: Internet
Author: User

CreateUniqueidentifierUnique value of type.

Syntax

NEWID ( )

Return type

Uniqueidentifier

Example A. Use the newid function for the variable

The following example uses newid to declareUniqueidentifierAssign values to variables of the data type. Print the value before testing it.UniqueidentifierThe value of the data type variable.

-- Creating a local variable with DECLARE/SET syntax.DECLARE @myid uniqueidentifierSET @myid = NEWID()PRINT 'Value of @myid is: '+ CONVERT(varchar(255), @myid)

The following is the result set:

Value of @myid is: 6F9619FF-8B86-D011-B42D-00C04FC964FF

DescriptionFor each computer, the value returned by newid is different. The displayed number only serves as an explanation.

B. Use newid In the CREATE TABLE statement

The following example createsUniqueidentifierData TypeCustAnd use newid to fill the default value in the table. When newid () is assigned a default value, each new row and existing row haveCust_idThe unique value of a column.

-- Creating a table using NEWID for uniqueidentifier data type.CREATE TABLE cust(cust_id uniqueidentifier NOT NULLDEFAULT newid(),company varchar(30) NOT NULL,contact_name varchar(60) NOT NULL,address varchar(30) NOT NULL,city varchar(30) NOT NULL,state_province varchar(10) NULL,postal_code varchar(10) NOT NULL,country varchar(20) NOT NULL,telephone varchar(15) NOT NULL,fax varchar(15) NULL)GO-- Inserting data into cust table.INSERT cust(cust_id, company, contact_name, address, city, state_province,postal_code, country, telephone, fax)VALUES(newid(), 'Wartian Herkku', 'Pirkko Koskitalo', 'Torikatu 38', 'Oulu', NULL,'90110', 'Finland', '981-443655', '981-443655')INSERT cust(cust_id, company, contact_name, address, city, state_province,postal_code, country, telephone, fax)VALUES(newid(), 'Wellington Importadora', 'Paula Parente', 'Rua do Mercado, 12', 'Resende', 'SP','08737-363', 'Brazil', '(14) 555-8122', '')INSERT cust(cust_id, company, contact_name, address, city, state_province,postal_code, country, telephone, fax)VALUES(newid(), 'Cactus Comidas para Ilevar', 'Patricio Simpson', 'Cerrito 333', 'Buenos Aires', NULL,'1010', 'Argentina', '(1) 135-5555', '(1) 135-4892')INSERT cust(cust_id, company, contact_name, address, city, state_province,postal_code, country, telephone, fax)VALUES(newid(), 'Ernst Handel', 'Roland Mendel', 'Kirchgasse 6', 'Graz', NULL,'8010', 'Austria', '7675-3425', '7675-3426')INSERT cust(cust_id, company, contact_name, address, city, state_province,postal_code, country, telephone, fax)VALUES(newid(), 'Maison Dewey', 'Catherine Dewey', 'Rue Joseph-Bens 532', 'Bruxelles', NULL,'B-1180', 'Belgium', '(02) 201 24 67', '(02) 201 24 68')GO
C. assign values using uniqueidentifier and variables

The following example declares local variables@ MyidIsUniqueidentifierData type. Then, assign values to the variable using the set statement.

DECLARE @myid uniqueidentifierSET @myid = 'A972C577-DFB0-064E-1189-0154C99310DAAC12'GO

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.