我有以下代码执行oracle视图,如下所示:
def run_query connection.exec_query( "SELECT * FROM TABLE(FN_REQRESP(#{type_param},#{search_type_param},#{tid_param},#{last_param},#{key_param},#{tran_id_param},#{num_param},#{start_date_param},#{end_date_param}))") end
SELECT * FROM TABLE(FN_REQRESP('ALL','ALL_TRAN','100007','',TO_DATE('27-January-2017','dd-MON-yy'),TO_DATE('31-January-2017','dd-MON-yy')))
所以,我试着添加一个prepare语句如下:
connection.exec_query('SELECT * FROM TABLE(FN_REQRESP(?,?,?))','myquery',[type_param,search_type_param,tid_param,last_param,key_param,tran_id_param,num_param,start_date_param,end_date_param])
我现在收到以下错误:
NoMethodError: undefined method `type’ for “‘ALL'”:String: SELECT *
FROM TABLE(FN_REQRESP(?,?))
这是单引号搞砸了我相信.有办法克服这个问题吗?
编辑:
我尝试了NDN的答案和下面的错误:
OCIError: ORA-00907: missing right parenthesis: SELECT * FROM TABLE(FN_REQRESP('\'ALL\'','\'ALL_TRAN\'','\'100007\'','\'\'','TO_DATE(\'01-February-2017\',\'dd-MON-yy\')','TO_DATE(\'10-February-2017\',\'dd-MON-yy\')'))
解决方法
看看
the source,绑定会以某种神奇的方式进行转换,你也必须传递一个名为prepare:true的参数.
在older versions,它也常常以不同的方式工作.
为了省去麻烦,你可以简单地使用#sanitize:
params = { type: type_param,search_type: search_type_param,tid: tid_param,last: last_param,key: key_param,tran_id: tran_id_param,num: num_param,start_date: start_date_param,end_date: end_date_param,} params.each do |key,param| params[key] = ActiveRecord::Base.sanitize(param) end connection.exec_query( "SELECT * FROM TABLE(FN_REQRESP(#{params[:type]},#{params[:search_type]},#{params[:tid]},#{params[:last]},#{params[:key]},#{params[:tran_id]},#{params[:num]},#{params[:start_date]},#{params[:end_date]}))" )