zillow / ctds

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

SqlVarChar wrapper truncate issue #11

Closed markoh1 closed 6 years ago

markoh1 commented 6 years ago

I'm unable to insert larger strings into table column varchar(max) using SqlVarChar wrapper. It seems that SqlVarChar truncates strings at 8000 chars. SqlNVarChar works as expected.

Tested with ctds 1.6.1 and freetds 1.00.78, on SQL server 2014 SP2

Demonstration (using select only):

conn = ctds.connect(server=server, database=database, user=user, password=password)
cur = conn.cursor()

# Example1
s = 'x' * 20000
cur.execute('select :0', (ctds.SqlVarChar(s),))
row = cur.fetchone()
print 'SqlVarChar: %s' % len(row[0]) # prints 8000

# Example 2
s = u'x' * 20000
cur.execute('select :0', (ctds.SqlNVarChar(s),))
row = cur.fetchone()
print 'SqlNVarChar: %s' % len(row[0]) # prints 20000
joshuahlang commented 6 years ago

The SqlNVarChar wrapper uses NTEXT internally when the wrapped value is longer than 4000 characters due to some issue in FreeTDS's implementation of (N)VARCHAR(MAX). The SqlVarChar wrapper should do the same for values longer than 8000 characters.

joshuahlang commented 6 years ago

Fixed in 1.6.2