uniqueidentifier (Transact-SQL)uniqueidentifier (Transact-SQL)

適用於: 是SQL Server 是Azure SQL Database 是Azure SQL 資料倉儲 是平行處理資料倉儲 APPLIES TO: yesSQL Server yesAzure SQL Database yesAzure SQL Data Warehouse yesParallel Data Warehouse

這是 16 位元組的 GUID。Is a 16-byte GUID.

RemarksRemarks

uniqueidentifier 資料類型的資料行或本機變數可以利用下列方法,初始化為一個值:A column or local variable of uniqueidentifier data type can be initialized to a value in the following ways:

  • 使用 NEWIDNEWSEQUENTIALID 函式。By using the NEWID or NEWSEQUENTIALID functions.
  • xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx 格式的字串常數轉換,其中每一個 x 是範圍 0-9 或 a-f 的十六進位數字。By converting from a string constant in the form xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, in which each x is a hexadecimal digit in the range 0-9 or a-f. 例如,6F9619FF-8B86-D011-B42D-00C04FC964FF 是有效的 uniqueidentifier 值。For example, 6F9619FF-8B86-D011-B42D-00C04FC964FF is a valid uniqueidentifier value.

比較運算子可以搭配使用 uniqueidentifier 值。Comparison operators can be used with uniqueidentifier values. 不過排序並不是比較兩值的位元模式加以實作的。However, ordering is not implemented by comparing the bit patterns of the two values. 唯一可以對 uniqueidentifier 值執行的作業,是比較 (=, <>, <, >, <=, >=) 以及檢查 NULL (IS NULL 和 IS NOT NULL)。The only operations that can be performed against a uniqueidentifier value are comparisons (=, <>, <, >, <=, >=) and checking for NULL (IS NULL and IS NOT NULL). 其他算術運算子一律不能使用。No other arithmetic operators can be used. 除了 IDENTITY 之外,所有的資料行條件約束和屬性,都可以用於 uniqueidentifier 資料類型。All column constraints and properties, except IDENTITY, can be used on the uniqueidentifier data type.

具有更新訂閱的合併式複寫和異動複寫,都使用 uniqueidentifier 資料行,以確保資料列可以在多份資料表唯一識別。Merge replication and transactional replication with updating subscriptions use uniqueidentifier columns to guarantee that rows are uniquely identified across multiple copies of the table.

轉換 Uniqueidentifier 資料Converting uniqueidentifier Data

uniqueidentifier 類型會基於轉換字元運算式的用途,而被視為字元類型;因此會受到轉換成字元類型之截斷規則的影響。The uniqueidentifier type is considered a character type for the purposes of conversion from a character expression, and therefore is subject to the truncation rules for converting to a character type. 亦即,將字元運算式轉換成不同大小的字元資料類型時,對新資料類型而言太大的值會被截斷。That is, when character expressions are converted to a character data type of a different size, values that are too long for the new data type are truncated. 請參閱<範例>一節。See the Examples section.

限制事項Limitations and restrictions

這些工具和功能不支援 uniqueidentifier 資料類型:These tools and features do not support the uniqueidentifier data type:

範例Examples

下列範例會將 uniqueidentifier 值轉換成 char 資料類型。The following example converts a uniqueidentifier value to a char data type.

DECLARE @myid uniqueidentifier = NEWID();  
SELECT CONVERT(char(255), @myid) AS 'char';  

下列範例會示範當值對於要轉換的目標資料類型而言太大時,資料的截斷方式。The following example demonstrates the truncation of data when the value is too long for the data type being converted to. 因為 uniqueidentifier 類型限制為 36 個字元,所以超過該長度的字元會被截斷。Because the uniqueidentifier type is limited to 36 characters, the characters that exceed that length are truncated.

DECLARE @ID nvarchar(max) = N'0E984725-C51C-4BF4-9960-E1C80E27ABA0wrong';  
SELECT @ID, CONVERT(uniqueidentifier, @ID) AS TruncatedValue;  

以下為結果集:Here is the result set.

String                                       TruncatedValue  
-------------------------------------------- ------------------------------------  
0E984725-C51C-4BF4-9960-E1C80E27ABA0wrong    0E984725-C51C-4BF4-9960-E1C80E27ABA0  
  
(1 row(s) affected)  

另請參閱See also

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)
DECLARE @local_variable (Transact-SQL)DECLARE @local_variable (Transact-SQL)
NEWID (Transact-SQL)NEWID (Transact-SQL)
NEWSEQUENTIALID (Transact-SQL) NEWSEQUENTIALID (Transact-SQL)
SET @local_variable (Transact-SQL)SET @local_variable (Transact-SQL)
Updatable Subscriptions for Transactional ReplicationUpdatable Subscriptions for Transactional Replication