数据类型 (Transact-SQL)Data types (Transact-SQL)

适用对象: 是SQL Server 是Azure SQL 数据库 是Azure Synapse Analytics (SQL DW) 是并行数据仓库 APPLIES TO: yesSQL Server yesAzure SQL Database yesAzure Synapse Analytics (SQL DW) yesParallel Data Warehouse

SQL ServerSQL Server 中,每个列、局部变量、表达式和参数都具有一个相关的数据类型。In SQL ServerSQL Server, each column, local variable, expression, and parameter has a related data type. 数据类型是一种属性,用于指定对象可保存的数据的类型:整数数据、字符数据、货币数据、日期和时间数据、二进制字符串等。A data type is an attribute that specifies the type of data that the object can hold: integer data, character data, monetary data, date and time data, binary strings, and so on.

SQL ServerSQL Server 提供系统数据类型集,该类型集定义了可与 SQL ServerSQL Server 一起使用的所有数据类型。supplies a set of system data types that define all the types of data that can be used with SQL ServerSQL Server. 还可以使用 Transact-SQLTransact-SQLMicrosoftMicrosoft .NET Framework.NET Framework 定义自己的数据类型。You can also define your own data types in Transact-SQLTransact-SQL or the MicrosoftMicrosoft .NET Framework.NET Framework. 别名数据类型基于系统提供的数据类型。Alias data types are based on the system-supplied data types. 有关别名数据类型的详细信息,请参阅 CREATE TYPE (Transact-SQL)For more information about alias data types, see CREATE TYPE (Transact-SQL). 用户定义类型从您使用 .NET Framework.NET Framework 支持的编程语言之一创建的类的方法和运算符中获取它们的特征。User-defined types obtain their characteristics from the methods and operators of a class that you create by using one of the programming languages support by the .NET Framework.NET Framework.

当两个具有不同数据类型、排序规则、精度、小数位数或长度的表达式通过运算符进行组合时,结果的特征由以下规则确定:When two expressions that have different data types, collations, precision, scale, or length are combined by an operator, the characteristics of result are determined by the following:

  • 结果的数据类型是通过将数据类型的优先顺序规则应用到输入表达式的数据类型来确定的。The data type of the result is determined by applying the rules of data type precedence to the data types of the input expressions. 有关详细信息,请参阅数据类型优先级 (Transact-SQL)For more information, see Data Type Precedence (Transact-SQL).
  • 当结果数据类型为 char、varchar、text、nchar、nvarchar 或 ntext 时,结果的排序规则由排序规则的优先顺序规则确定。The collation of the result is determined by the rules of collation precedence when the result data type is char, varchar, text, nchar, nvarchar, or ntext. 有关详细信息,请参阅排序规则优先顺序 (Transact-SQL)For more information, see Collation Precedence (Transact-SQL).
  • 结果的精度、小数位数及长度取决于输入表达式的精度、小数位数及长度。The precision, scale, and length of the result depend on the precision, scale, and length of the input expressions. 有关详细信息,请参阅精度、小数位数和长度 (Transact-SQL)For more information, see Precision, Scale, and Length (Transact-SQL).

SQL ServerSQL Server 提供了数据类型同义词,以便实现 ISO 兼容性。provides data type synonyms for ISO compatibility. 有关详细信息,请参阅数据类型同义词 (Transact-SQL)For more information, see Data Type Synonyms (Transact-SQL).

数据类型类别Data type categories

SQL ServerSQL Server 中的数据类型归纳为下列类别:Data types in SQL ServerSQL Server are organized into the following categories:

精确数字Exact numerics Unicode 字符串Unicode character strings
近似数字Approximate numerics 二进制字符串Binary strings
日期和时间Date and time 其他数据类型Other data types
字符串Character strings

SQL ServerSQL Server 中,根据其存储特征,某些数据类型被指定为属于下列各组:In SQL ServerSQL Server, based on their storage characteristics, some data types are designated as belonging to the following groups:

  • 大值数据类型:varchar(max) 和 nvarchar(max)Large value data types: varchar(max), and nvarchar(max)

  • 大型对象数据类型:text、ntext、image、varbinary(max) 和 xmlLarge object data types: text, ntext, image, varbinary(max), and xml

    备注

    sp_help 返回 -1 作为较大值数据类型和 xml 数据类型的长度。sp_help returns -1 as the length for the large-value and xml data types.

精确数字Exact numerics

bigintbigint numericnumeric
bitbit smallintsmallint
decimaldecimal smallmoneysmallmoney
intint tinyinttinyint
moneymoney

近似数字Approximate numerics

floatfloat realreal

日期和时间Date and time

datedate datetimeoffsetdatetimeoffset
datetime2datetime2 smalldatetimesmalldatetime
datetimedatetime timetime

字符串Character strings

charchar varcharvarchar
texttext

Unicode 字符串Unicode character strings

ncharnchar nvarcharnvarchar
ntextntext

二进制字符串Binary strings

binarybinary varbinaryvarbinary
图像image

其他数据类型Other data types

cursorcursor rowversionrowversion
hierarchyidhierarchyid uniqueidentifieruniqueidentifier
sql_variantsql_variant xmlxml
空间几何类型Spatial Geometry Types 空间地理类型Spatial Geography Types
tabletable

另请参阅See also

CREATE PROCEDURE (Transact-SQL)CREATE PROCEDURE (Transact-SQL)
CREATE TABLE (Transact-SQL)CREATE TABLE (Transact-SQL)
DECLARE @local_variable (Transact-SQL) EXECUTE (Transact-SQL)DECLARE @local_variable (Transact-SQL) EXECUTE (Transact-SQL)
表达式 (Transact-SQL)Expressions (Transact-SQL)
函数 (Transact-SQL)Functions (Transact-SQL)
LIKE (Transact-SQL)LIKE (Transact-SQL)
sp_droptype (Transact-SQL)sp_droptype (Transact-SQL)
sp_help (Transact-SQL)sp_help (Transact-SQL)
sp_rename (Transact-SQL)sp_rename (Transact-SQL)