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

适用对象: yesSQL ServeryesAzure SQL 数据库yesAzure SQL 数据仓库yes并行数据仓库APPLIES TO: yesSQL Server yesAzure SQL Database yesAzure SQL Data Warehouse yesParallel Data Warehouse

16 字节 GUID。Is a 16-byte GUID.


uniqueidentifier 数据类型的列或局部变量可通过以下方式初始化为一个值 :A column or local variable of uniqueidentifier data type can be initialized to a value in the following ways:

  • 通过从 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:


以下示例将 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

CAST 和 CONVERT (Transact-SQL)CAST and CONVERT (Transact-SQL)
数据类型 (Transact-SQL)Data Types (Transact-SQL)
DECLARE @local_variable (Transact-SQL)DECLARE @local_variable (Transact-SQL)
NEWID (Transact-SQL)NEWID (Transact-SQL)
SET @local_variable (Transact-SQL)SET @local_variable (Transact-SQL)
Updatable Subscriptions for Transactional ReplicationUpdatable Subscriptions for Transactional Replication