rails - 调用oracle存储过程
访问量: 3472
参考: http://www.gakshay.com/2013/08/25/call-stored-procedure-in-sequel/
oracle的存储过程跟mysql的很不一样.
调试:
1. 建议先在 pl/sql 中调试.
点击存储过程, 然后在右键弹出的菜单中,选择 测试. 输入对应的输入参数,就可以看到 out 参数的值. (可以看到这里使用了类似ruby hash的写法...实际上我也不清楚是如何转换成SQL的,总之它跟 mysql的存储过程SQL完全不一样就对了,)
另外, sql developer是使用exec 来执行的.
pl/sql 是使用 begin ... end 来执行的.
mysql 和标准的sql 是使用 call来执行的.
2. 然后, 在plsql的 "SQL窗口"中调试.
在SQL窗口中调试的话, 就需要用这样的格式:
// 注意要先为 out参数做声明, 然后下面才能使用. 不能直接用 @result 这样的形式. declare result int; begin -- Call the procedure sp_prs_createcusquote(result,147,1078,395,1); dbms_output.put_line(result); end;
3. 然后,在 rails中, 就可以使用了. 代码如下: (这是rails中的脚本,可以使用 bundle exec ruby ... 来直接运行)
ENV['RAILS_ENV'] = ARGV.first || ENV['RAILS_ENV'] || 'production' require File.expand_path(File.dirname(__FILE__) + "/../config/environment") require 'rails' require 'rubygems' # 注意,这里的sql语句中,只有begin ... end 就可以了,不需要有 declare. 另外, sql = %{ begin sp_prs_createcusquote(:result,147,1078,395,1); end; } # 注意,这里用的是这个语法,connection.raw_connection.parse ... cursor = ActiveRecord::Base.connection.raw_connection.parse sql cursor.bind_param(:result, nil, Fixnum) cursor.exec() puts cursor[:result]