rails - 调用mysql存储过程

访问量: 238

对于mysql: 

注意: 这个可以:(执行一行, 就要写一次)

ActiveRecord::Base.connection.execute "call proc_adder(1333,33, @result)"
ActiveRecord::Base.connection.exec_query "select @result as r"

总之,如果要"仅仅是执行", 就是execute方法, 

如果要查询结果, 就要 exec_query方法.

下面是我测试时使用的完整的代码: 

irb(main):014:0> ActiveRecord::Base.connection.execute "set @b = 222;"
   (0.2ms)  set @b = 222;
=> nil
irb(main):015:0> ActiveRecord::Base.connection.execute "call proc_adder(1333,@b, @result)"
   (0.5ms)  call proc_adder(1333,@b, @result)
=> nil
irb(main):016:0> result = ActiveRecord::Base.connection.exec_query "select @result as r"
  SQL (0.5ms)  select @result as r
=> #

跟 普通的sql语句是一样的:

注意: 这样的写法是不行的. 

sql = "call proc_adder(1,33, @result); select @result as result;"  

ActiveRecord::Base.connection.exec_query sql

遗憾的是 目前暂时不能运行. (虽然上面的sql语言可以直接在 mysql workbench中运行)

mysql> call proc_adder(1,33, @result); select @result as result;
Query OK, 0 rows affected (0.00 sec)

+--------+
| result |
+--------+
|     34 |
+--------+
1 row in set (0.00 sec)

订阅/RSS Feed

Subscribe

分类/category