2

I am attempting to execute a stored procedure via a pyodbc cursor.

cursor.execute('EXEC MyProc ?', (None))

Where MyProc takes a VARBINARY parameter.

However, I get this error from FreeTDS:

pyodbc.DataError: ('22018', '[22018] [FreeTDS][SQL Server]Implicit conversion from data type varchar to varbinary is not allowed. Use the CONVERT function to run this query. (257) (SQLExecDirectW)')

Only other discussion of this issue I found on this very old thread.

Any ideas how to implement this?

My environment notes:

  • Python code running on a Linux server
  • Python 3.5
  • pyodbc 3.0.10
  • MS SQL Server 2014
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
joeb1415
  • 527
  • 2
  • 7
  • 12
  • Can you try `cursor.execute('EXEC MyProc @MyParam = ?', None)`? I've got a working example without a VARBINARY calling in that fashion. – FlipperPA May 24 '16 at 18:42
  • 1
    I see that your error message refers to FreeTDS. Are you running this code on a non-Windows machine? FWIW, I am unable to reproduce your issue under Windows with `Driver={SQL Server Native Client 10.0}`. – Gord Thompson May 24 '16 at 23:42
  • @FlipperPA, No luck, unfortunately! Same error via: `cursor.execute('exec Test.dbo.spVarbinaryTest @Varbinary=?', None)`. FWIW, passing a valid byte string does work in the named parameter syntax too: `cursor.execute('exec Test.dbo.spVarbinaryTest @Varbinary=?', b'1234')` – joeb1415 May 25 '16 at 01:06
  • @GordThompson indeed! Both syntaxes (named and unnamed params) work on Windows. I am running this on a linux server though. Tried on OSX and get the same error. Good tip though that this might be FreeTDS related -- I'll add that tag to the question. – joeb1415 May 25 '16 at 01:21
  • @joeb1415 Can you include you freetds.conf, specifically which TDS version you are using? – FlipperPA May 25 '16 at 02:31
  • FreeTDS version: `/usr/local/etc/freetds.conf -> ../Cellar/freetds/0.95.69/etc/freetds.conf`; On TDS version: `[global] tds version = 8.0` – joeb1415 May 25 '16 at 19:53
  • I have the same exact error with python 2.7 and linux using FreeTDS. If I pass bytearray(0) from python, instead of None, it works. I want None (Null) though. – user3885927 Mar 01 '18 at 00:07

0 Answers0