Search code examples
rubysql-serverodbcdbifreetds

ruby+odbc+freetds MSSQL error negative string size


so I'm executing queries in Ruby to a MS SQL database, and all is well. I've got FreeTDS/ODBC setup and I'm laughing. Out of no where, I get this error on one of my queries:

/home/jarrett/.rvm/gems/ruby-1.9.2-p318/gems/dbd-odbc-0.2.5/lib/dbd/odbc/statement.rb:41:in `fetch': negative string size (or size too big) (ArgumentError)
            from /home/jarrett/.rvm/gems/ruby-1.9.2-p318/gems/dbd-odbc-0.2.5/lib/dbd/odbc/statement.rb:41:in `fetch'
            from /home/jarrett/.rvm/gems/ruby-1.9.2-p318/gems/dbi-0.4.5/lib/dbi/handles/statement.rb:220:in `fetch'

What the? Ok, so other queries are fine, but for some reason this one bonks. The query is 'select * from [Provider]'. Pretty straight forward. The query runs on the MS SQL Server just fine. This query used to run on another machine I had setup with Ruby + FreeTDS + ODBC. Hmm..

I then make a quick ruby file with this:

#!/usr/bin/env ruby

require 'dbi'

db = DBI.connect('dbi:ODBC:MYDB', 'blahblah', 'blahblah')

select = db.prepare('select * from [Provider]')
select.execute
while rec = select.fetch do
puts rec.to_s
end
db.disconnect

It bonks. Then I select just a few columns (instead of *), like so:

#!/usr/bin/env ruby

require 'dbi'

db = DBI.connect('dbi:ODBC:MYDB', 'blahblah', 'blahblah')

select = db.prepare('select Address1, Address2 from [Provider]')
select.execute
while rec = select.fetch do
puts rec.to_s
end
db.disconnect

And it works fine! So, I change my query to just select the individual columns (only using about 13 columns in my query), and it's working fine. I counted the number of total columns in the table, and there are 103.

So, I guess I'm curious: why does this bonk on me? Is 103 columns too many to handle?

I'm running on a 64 bit Debian machine, connecting to the office via VPN (MS SQL Server machine is on office LAN). First time I've seen an error like this.

Anyone have any ideas?


Solution

  • dbi + dbd-odbc is very old and it seems not "up to date".

    You should give TinyTDS a try, which implements the TDS protocol directly for ruby:

    https://github.com/rails-sqlserver/tiny_tds