NEWID (Transact-SQL)NEWID (Transact-SQL)

适用于: 是SQL Server是Azure SQL 数据库是Azure SQL 数据仓库否并行数据仓库APPLIES TO: yesSQL Server yesAzure SQL Database yesAzure SQL Data Warehouse noParallel Data Warehouse

创建 uniqueidentifier 类型的唯一值。Creates a unique value of type uniqueidentifier.

主题链接图标 TRANSACT-SQL 语法约定Topic link icon Transact-SQL Syntax Conventions

语法Syntax

  
NEWID ( )  

返回类型Return Types

uniqueidentifieruniqueidentifier

RemarksRemarks

NEWID() 遵从 RFC4122。NEWID() is compliant with RFC4122.

示例Examples

A.A. 对变量使用 NEWID 函数Using the NEWID function with a variable

以下示例使用 NEWID() 对声明为 uniqueidentifier 数据类型的变量赋值。The following example uses NEWID() to assign a value to a variable declared as the uniqueidentifier data type. 在测试 uniqueidentifier 数据类型变量的值之前,先打印该值。The value of the uniqueidentifier data type variable is printed before the value is tested.

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

下面是结果集:Here is the result set.

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

备注

NEWID 对每台计算机返回的值各不相同。The value returned by NEWID is different for each computer. 所显示的数字仅起解释说明的作用。This number is shown only for illustration.

B.B. 在 CREATE TABLE 语句中使用 NEWIDUsing NEWID in a CREATE TABLE statement

适用范围:SQL ServerSQL ServerApplies to: SQL ServerSQL Server

以下示例创建数据类型为 uniqueidentifier 的 cust 表,并使用 NEWID 作为默认值填充该表。The following example creates the cust table with a uniqueidentifier data type, and uses NEWID to fill the table with a default value. NEWID() 赋予默认值时,每个新行和现有行均对 CustomerID 列具有唯一值。In assigning the default value of NEWID(), each new and existing row has 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 5 rows into cust table.  
INSERT cust  
(CustomerID, Company, ContactName, Address, City, StateProvince,   
 PostalCode, CountryRegion, Telephone, Fax)  
VALUES  
 (NEWID(), 'Wartian Herkku', 'Pirkko Koskitalo', 'Torikatu 38', 'Oulu', NULL,  
 '90110', 'Finland', '981-443655', '981-443655')  
,(NEWID(), 'Wellington Importadora', 'Paula Parente', 'Rua do Mercado, 12', 'Resende', 'SP',  
 '08737-363', 'Brasil', '(14) 555-8122', '')  
,(NEWID(), 'Cactus Comidas para Ilevar', 'Patricio Simpson', 'Cerrito 333', 'Buenos Aires', NULL,   
 '1010', 'Argentina', '(1) 135-5555', '(1) 135-4892')  
,(NEWID(), 'Ernst Handel', 'Roland Mendel', 'Kirchgasse 6', 'Graz', NULL,  
 '8010', 'Austria', '7675-3425', '7675-3426')  
,(NEWID(), 'Maison Dewey', 'Catherine Dewey', 'Rue Joseph-Bens 532', 'Bruxelles', NULL,  
 'B-1180', 'Belgium', '(02) 201 24 67', '(02) 201 24 68');  
GO  

C.C. 使用 uniqueidentifier 和变量赋值Using uniqueidentifier and variable assignment

以下示例将名为 @myid 的局部变量声明为 uniqueidentifier 数据类型的变量。The following example declares a local variable called @myid as a variable of uniqueidentifier data type. 然后使用 SET 语句为该变量赋值。Then, the variable is assigned a value by using the SET statement.

DECLARE @myid uniqueidentifier ;  
SET @myid = 'A972C577-DFB0-064E-1189-0154C99310DAAC12';  
SELECT @myid;  
GO  

另请参阅See Also

NEWSEQUENTIALID (Transact-SQL) NEWSEQUENTIALID (Transact-SQL)
ALTER TABLE (Transact-SQL) ALTER TABLE (Transact-SQL)
CAST 和 CONVERT (Transact-SQL) CAST and CONVERT (Transact-SQL)
CREATE TABLE (Transact-SQL) CREATE TABLE (Transact-SQL)
数据类型 (Transact-SQL) Data Types (Transact-SQL)
System Functions (Transact-SQL) System Functions (Transact-SQL)
uniqueidentifier (Transact-SQL) uniqueidentifier (Transact-SQL)
序列号Sequence Numbers