tiny_tds failed at the second execute
-
16-04-2021 - |
Question
Today, tiny_tds suddenly does not accept more than one execute
and returns:
C:\>ruby test_use.rb
one
two
C:/test_use.rb:15:in `execute': Attempt to initiate a new Adaptive Server operation with results pending (TinyTds::Error)
from C:/test_use.rb:15
The code is simply as three USE
s:
require 'rubygems'
require 'yaml'
require 'fastercsv'
require 'tiny_tds'
require 'iconv'
CONFIG = YAML.load_file("config.yml")
client = TinyTds::Client.new(:username => CONFIG["db"]["username"], :password => CONFIG["db"]["password"],
:host => CONFIG["db"]["server"], :database => CONFIG["db"]["database"])
puts "one"
client.execute("USE DATAFEED")
puts "two"
client.execute("USE DATAFEED")
puts "three"
client.execute("USE DATAFEED")
Any clue what is the problem? I tried rebooting the Windows machine already.
Solution
You have to terminate the execute with a do
:
Client.execute("...").do
OTHER TIPS
Here is an example of how I do it.
results = $regcenter_db.execute("select top 10 * from events")
event_ids = results.collect { |i| i["event_id"] }
results.do
You have to call do
OR cancel
. If you are doing something like getting a partial results, and you are not interested in execute the do
method you can call cancel
to abort the query.
According to the documentation for TinyTds on GitHub:
It is important that you either return the data from the query, most likely with the #each method, or that you cancel the results before asking the client to execute another SQL batch. Failing to do so will yield an error.
To cancel the query, use the cancel
method:
client.execute("USE DATAFEED").cancel