An OLE DB provider exposes the data types of its data in terms of OLE DB type identifiers called DBTYPEs. There are two ways that data types are converted between OLE DB data types and native Microsoft® SQL Server™ data types:
This conversion occurs when SQL Server reads data from the OLE DB data source, either in SELECT statements or in the reading side of UPDATE, INSERT, or in DELETE statements.
This conversion occurs when SQL Server writes data, mostly in INSERT or UPDATE statements, into the OLE DB data source in which the modified table is a remote table.
This mapping defines the allowed comparisons and expressions, and the valid explicit conversions involving remote data. The data type of remote_column corresponds to the mapped local data type as shown in the following table.
The type rules for remote table columns in expressions can be described by this rule: A given remote column value is legal in a Transact-SQL expression if the corresponding mapped SQL Server data type in the following table is legal in the same context. For example, consider the expression:
local_column operator remote_column
local_column is a local table column and remote_column is a remote table column. This is a valid expression if operator is a valid operator for the local column’s data type and for the data type to which the DBTYPE of remote_column maps.
Similarly, CAST(remote_column AS data_type_1) is allowed if the DBTYPE of remote_column maps to the SQL Server native data_type_2 and explicit conversion from data_type_2 to data_type_1 is allowed. For example, a column of data type DBTYPE_DATE on the provider side can be converted to a datetime column in SQL Server. However, the DBTYPE_DATE data cannot be converted directly to varchar.
The mapping to a SQL Server type is determined by the DBTYPE and the DBCOLUMNFLAGS values describing the column. This information comes from the provider through either the COLUMNS schema rowset, or through the IColumnsInfo interface. In the case of the COLUMNS schema rowset, the DATA_TYPE and COLUMN_FLAGS columns represent these values. In the case of the IColumnsInfo::GetColumnInfo interface, the wType and dwFlags members of the DBCOLUMNINFO structure represent this information.
The table shows data type mappings from the OLE DB provider to SQL Server. For a given column, given its DBTYPE and its DBCOLUMNFLAG value, the corresponding SQL Server data type can be found.
DBTYPE |
DBCOLUMNFLAGS |
SQL Server data type |
---|---|---|
DBTYPE_I1 | numeric(3, 0)1 | |
DBTYPE_I2 | smallint | |
DBTYPE_I4 | int | |
DBTYPE_I8 | numeric(19,0) | |
DBTYPE_UI1 | tinyint | |
DBTYPE_UI2 | numeric(5,0) | |
DBTYPE_UI4 | numeric(10,0) | |
DBTYPE_UI8 | numeric(19,0) | |
DBTYPE_R4 | float | |
DBTYPE_R8 | real | |
DBTYPE_NUMERIC | numeric | |
DBTYPE_DECIMAL | decimal | |
DBTYPE_CY | money | |
DBTYPE_BSTR | DBCOLUMNFLAGS_ISLONG = true | ntext |
DBTYPE_BSTR | DBCOLUMNFLAGS_ISFIXEDLENGTH = true | nchar |
DBTYPE_BSTR | DBCOLUMNFLAGS_ISFIXEDLENGTH = false | nvarchar |
DBTYPE_IDISPATCH | Error | |
DBTYPE_ERROR | Error | |
DBTYPE_BOOL | bit | |
DBTYPE_VARIANT | nvarchar(4000) | |
DBTYPE_IUNKNOWN | Error | |
DBTYPE_GUID | uniqueidentifier | |
DBTYPE_BYTES | DBCOLUMNFLAGS_ISLONG = true or Maximum column size > 8,000 bytes | image |
DBTYPE_BYTES | DBCOLUMNFLAGS_ISROWVER = true, DBCOLUMNFLAGS_ISFIXEDLENGTH = true, Column size = 8 |
timestamp |
DBTYPE_BYTES | DBCOLUMNFLAGS_ISFIXEDLENGTH = true | binary |
DBTYPE_BYTES | DBCOLUMNFLAGS_ISFIXEDLENGTH = false | varbinary |
DBTYPE_STR | DBCOLUMNFLAGS_ISFIXEDLENGTH = true | char |
DBTYPE_ STR | DBCOLUMNFLAGS_ISFIXEDLENGTH = false | varchar |
DBTYPE_STR | DBCOLUMNFLAGS_ISLONG = true or Maximum column size > 4,000 characters | text |
DBTYPE_WSTR | DBCOLUMNFLAGS_ISFIXED | nchar |
DBTYPE_WSTR | DBCOLUMNFLAGS_ISFIXEDLENGTH = false | nvarchar |
DBTYPE_WSTR | DBCOLUMNFLAGS_ISLONG = true or Maximum column size > 4,000 characters | ntext |
DBTYPE_UDT | Error | |
DBTYPE_DATE | datetime | |
DBTYPE_DBDATE | Error | |
DBTYPE_DBTIME | Error | |
DBTYPE_DBTIMESTAMP | datetime | |
DBTYPE_ARRAY | Error | |
DBTYPE_BYREF | Ignored | |
DBTYPE_VECTOR | Error | |
DBTYPE_RESERVED | Error | |
1 numeric(p,s) indicates SQL Server data type numeric with precision p and scale s. |
Note If the data must be converted to a native data type different from the shown default, then an explicit conversion (using either the CAST or CONVERT function) is required. For more information, see CAST and CONVERT.
Native Microsoft SQL Server data types map to OLE/DB types using the same table name. A mapping from a SQL Server type S1 to a given OLE/DB type T is allowed if either of these exist: