How to get output parameters from MySQL stored procedure in Rails?

拈花ヽ惹草 提交于 2019-12-03 15:19:47

I found a way to get the output parameter, to share with you guys, see below:

1 to create a connection

client = Mysql2::Client.new(ActiveRecord::Base.configurations[Rails.env.to_s].symbolize_keys)

2 use this connection to call your sp

client.query "call sp_xxx"

3 to get the output parameter

result = client.query("select @output_parameter")

4 return the output

return !result.first["@output_parameter"].nil?

MySQL doesnt work like SQL Server. If you want a store procedure with a return value, that you really want it's a function (UDF). You need specify the RETURN like any C++ function. Also you dont need use call, because its a function. Just use SELECT function()

Try to look up info about UDF in MySQL manual. There is very explicity help for do that.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!