Search code examples
ruby-on-railssql-server-2008ruby-on-rails-4tiny-tds

TinyTDS: chek if query succeded


I am using tiny_tds gem in a rails project (working with a legacy MSSQL 2008 database)

I execute the following query:

update_comment_result = self.get_pmi_client.execute("UPDATE UECOMCLI SET COMMENT = '" + params[:comment] + "' " +
        "WHERE ECKTSOC = '" + params[:lcktsoc] + "' AND ECKTNUMERO = '" + params[:lcktnumero] + "' AND ECKTINDICE = '" + params[:lcktpsf] + "' \n" +
        "IF @@ROWCOUNT = 0 \n
        INSERT INTO UECOMCLI (ECKTSOC, ECKTNUMERO, ECKTINDICE, COMMENT) VALUES ('" + params[:lcktsoc] + "', '" + params[:lcktnumero] + "', '" + params[:lcktpsf] + "', '" + params[:comment] + "')")

After that, I am unable to find out if the query succeded or not. I have tried using:

update_comment_result.affected_rows

which returns -1

I also tried

update_comment_result.each
update_comment_result.return_code

which returns nil

and

update_comment_result.each
update_comment_result.affected_rows

which returns -1

The query succeeds because the change is made in the database, so why the faulty results?

How can I check if the query succeeded?


Solution

  • When you call 'execute' method, your sql does not actually run. TinyTDS sends it to sql server only when you begin manipulating results, and if there is an error, you can catch it

    res = Client.Execute('sql script')
    begin
        res.entries #now sql script is being sent to the sql server
    rescue => e
        puts e
    end