Search code examples
rubysequel

Stored Procedure return codes in Sequel (Ruby ORM)


In MS SQL Server a stored procedure can return an integer value called a return code to indicate the execution status of a procedure.

Does Sequel support this?

The value returned from my_stored_proc.call() does not match the integer I am returning in SQL. Is there some other way to access this?


Solution

  • The response from Jeremy Evans on the Sequel Talk Google Group was:

    Unfortunately, Sequel's support for stored procedures is fairly limited. I don't believe it supports return codes or in/out variables. It's designed similarly to the prepared statement support, where delete/update returns the number of affected rows, and select yields rows.

    If Sequel's support doesn't meet your needs, use Database#synchronize to get to the underlying database connection, and operate directly on that using the connection's API (which depends on the driver in use).

    "Getting the Return Value from JDBC MSSQL" describes how to do it from a Java perspective.

    And, here is a (working) first stab at a Ruby implementation:

    return_code = @db.synchronize do |conn|
      stmnt = conn.prepareCall('{ ? = call dbo.sp_sequel_test() }')
      stmnt.registerOutParameter(1, java::sql::Types::INTEGER)
      stmnt.execute
      # output parameters have not yet been processed, must call getMoreResults() first.
      stmnt.getMoreResults  
      stmnt.getInt(1)
    end