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

適用於: 是SQL Server 是Azure SQL Database 是Azure Synapse Analytics (SQL DW) 否平行處理資料倉儲 APPLIES TO: yesSQL Server yesAzure SQL Database yesAzure Synapse Analytics (SQL DW) 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)
系統函數 (Transact-SQL) System Functions (Transact-SQL)
uniqueidentifier (Transact-SQL) uniqueidentifier (Transact-SQL)
序號Sequence Numbers