【发布时间】:2011-09-20 05:36:15
【问题描述】:
我正在尝试从 MySQL 存储过程中获取输出参数,让我们看一个下面的示例:
1 在 mysql 中,我创建了这个过程,它可以工作。
CREATE PROCEDURE sp_name (out id int)
Begin
select id into @id from table order by id desc limit 1;
End
mysql> call sp_deduct_credit_and_money(@id);
Query OK, 0 rows affected (0.01 sec)
mysql> select @id;
+--------------+
| @id |
+--------------+
| 24 |
+--------------+
1 row in set (0.00 sec)
2 因此,它也适用于 Rails,但它不会为我返回任何值:
ActiveRecord::Base.connection.execute("call sp_name(@id)")
ActiveRecord::Base.connection.execute("select @id")
这是 Rails 中的日志
(2.0ms) call sp_name(@id)
(0.1ms) select @id
我的问题是,我怎样才能得到输出参数@id的返回值?
【问题讨论】:
-
使用存储过程的真正原因是什么?有特殊要求吗?如果是这样,不要忘记在整个开发团队中保持版本控制和维护是很困难的
-
你用什么 gem 做 mysql ?
-
@Fivell 我正在使用 mysql2
标签: mysql ruby-on-rails stored-procedures output-parameter