如何在rails上的ruby中调用存储过程?

时间:2012-09-19 14:31:58

标签: ruby-on-rails stored-procedures sqlanywhere

我是ROR的新手。我想在单击VIEW中的提交按钮时调用存储过程进行处理。

    Model:
    -------

    class Pro::DataImport < ActiveRecord::Base
      attr_accessible :file_name, :process_name, :updated_by, :validates

    end

    Controller:
    -----------------

    class Pro::DataImportsController < ApplicationController
       before_filter :authenticate_user!
      layout "layouts/enr/energy_master"

      def index
       @pro_data_imports = Pro::DataImport.all 
      end

      def new
        @pro_data_import = Pro::DataImport.new
      end

    end

    View
    ----------

     <%= form_for @pro_data_import do %>

  <div class="field">
    Browse the file to upload:<br />
    <%= file_field_tag ':file_name' %>
  </div>

  <div class="actions">
    <%= submit_tag 'Import File' %>
  </div>
<% end %>


    Stored Proc
    ---------------

    ALTER PROCEDURE "DBA"."my_enr_test"(file_name long varchar)
    BEGIN
        INSERT INTO DBA.pro_data_imports(file_name) values(file_name);
    END

先谢谢..请帮助我。我想从上传按钮获取文件路径并存储到数据库列file_name中。如何执行提交按钮的存储过程。请帮帮我!!

2 个答案:

答案 0 :(得分:9)

如果您使用的是ActiveRecord SQLServer适配器,请结帐:

http://rubydoc.info/gems/activerecord-sqlserver-adapter/3.2.9/ActiveRecord/ConnectionAdapters/Sqlserver/DatabaseStatements:execute_procedure

在您的代码中执行类似的操作

class Pro::DataImport < ActiveRecord::Base
  def self.update(user)
    self.execute_procedure("Stored Procedure Name", arg1, arg2)
  end
end

每个普通的SQL查询都会转换为要执行的存储过程。这就是ActiveRecord的SQL Server适配器的工作方式。因此,您只需担心数据库中定义的永久存储过程。

答案 1 :(得分:0)

# PL/SQL records or object type parameters should be passed as Hash

p_employee = { :employee_id => 1, :first_name => 'First', :last_name => 'Last', :hire_date => Time.local(2000,01,31) }
 plsql.test_full_name(p_employee)

# test_full_name is procedure name in oracle
# p_employee holds parameter list 
#employee_id is first param defined in stored procedure