sql_variant can be used in columns, parameters, variables, and the return values of user-defined functions. sql_variant enables these database objects to support values of other data types.
A column of type sql_variant may contain rows of different data types. For example, a column defined as sql_variant can store int, binary, and char values. The following table lists the types of values that cannot be stored by using sql_variant:
|
varchar(max)
|
varbinary(max)
|
|
nvarchar(max)
|
xml
|
|
text
|
ntext
|
|
image
|
timestamp
|
|
sql_variant
|
User-defined types
|
|
hierarchyid
|
|
sql_variant can have a maximum length of 8016 bytes. This includes both the base type information and the base type value. The maximum length of the actual base type value is 8,000 bytes.
A sql_variant data type must first be cast to its base data type value before participating in operations such as addition and subtraction.
sql_variant can be assigned a default value. This data type can also have NULL as its underlying value, but the NULL values will not have an associated base type. Also, sql_variant cannot have another sql_variant as its base type.
A unique, primary, or foreign key may include columns of type sql_variant, but the total length of the data values that make up the key of a specific row should not be more than the maximum length of an index. This is 900 bytes.
A table can have any number of sql_variant columns.
sql_variant cannot be used in CONTAINSTABLE and FREETEXTTABLE.
ODBC does not fully support sql_variant. Therefore, queries of sql_variant columns are returned as binary data when you use Microsoft OLE DB Provider for ODBC (MSDASQL). For example, a sql_variant column that contains the character string data 'PS2091' is returned as 0x505332303931.