zillow / ctds

Python DB-API 2.0 library for MS SQL Server
MIT License
83 stars 12 forks source link

Ctds assumes wrong datatypes on cursor.executemany() #43

Closed gizmo93 closed 5 years ago

gizmo93 commented 5 years ago

System Specs:

When inserting a sequence of Integers using .executemany(), ctds assumes the wrong data type, if the first integer is small.

Example:

CREATE TABLE TestTable
(
    Number INT NULL,
)
with ctds.connect('server', database='database') as con:
    with con.cursor() as cursor:

        data = [(1,), (30000,), (0, )]

        query = 'INSERT INTO TestTable (Number) VALUES (:0)'
        cursor.executemany(query, data)
        con.commit()

leads to" _tds.DataError: Error converting data type smallint to tinyint.".

The reason is the created INSERT Statement, which assumes datatype tinyint for this column, as we can see in SQL Server Profiler:

exec sp_executesql N'INSERT INTO TestTable (Number) VALUES (@param0)',N'@param0 TINYINT',1
go
exec sp_executesql N'INSERT INTO TestTable (Number) VALUES (@param0)',N'@param0 TINYINT',30000
go
joshuahlang commented 5 years ago

Fixed in 1.9.0