如何在Rails 3.2中使用OUT参数调用存储过程 - 更新

时间:2012-10-15 07:25:55

标签: mysql ruby-on-rails

我在MYSQL中创建了以下SP

CREATE PROCEDURE usp_BookDetailsCount(OUT bookCount INT)
BEGIN
       SELECT count(*) INTO bookCount 

       FROM books;

       SELECT bookCount;
END $$

我正在尝试使用rails 3.2.8来获取SP的参数值。

我在模型中的代码

count = 0 
 r = ActiveRecord::Base.connection.select_value("CALL usp_BookDetailsCount(OUT count)")   
 return count

我在浏览器中出现以下错误

Mysql::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'OUT count)' at line 1: CALL usp_BookDetailsCount(OUT count)

但是我能够在rails

中获得没有OUT参数的存储过程值

1 个答案:

答案 0 :(得分:0)

我担心没有特定的AR API方法与存储过程进行交互。试试#execute:

http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/DatabaseStatements.html#method-i-execute