Python 与 SQL Server 之间的数据类型映射

适用于: SQL Server 2017 (14.x) 及更高版本 Azure SQL 托管实例

本文列出了在 SQL Server 机器学习服务中使用 Python 集成功能时所支持的数据类型以及所执行的数据类型转换。

与 SQL Server 相比,Python 支持的数据类型数量有限。 因此,每当在 Python 脚本中使用 SQL Server 中的数据时,SQL 数据都可能会隐式转换为兼容的 Python 数据类型。 但是,通常无法自动执行精确的转换并将返回错误。

Python 和 SQL 数据类型

下表列出了提供的隐式转换。 不支持其他数据类型。

SQL 类型 Python 类型 说明
bigint float64
binary bytes
bit bool
char str
date datetime
datetime datetime 在 SQL Server 2017 CU6 及更高版本中受支持(支持类型为 datetime.datetime 的 NumPy 数组或 Pandaspandas.Timestamp)。 sp_execute_external_script 现在支持带有小数秒的 datetime 类型。
float float64
nchar str
nvarchar str
nvarchar(max) str
真实 float64
smalldatetime datetime
smallint int32
tinyint int32
uniqueidentifier str
varbinary bytes
varbinary(max) bytes
varchar(n) str
varchar(max) str