LoginSignup
20

More than 5 years have passed since last update.

Railsでストアドプロシージャを定義する例

Last updated at Posted at 2014-07-18

Migration中に記述する例

rails generate migration AAAProcedure

clas AAAProcedure < ActiveREcord::Migration
  def self.up
    execute <<-SQL
CREATE PROCEDURE my_procedure (IN in_param INTEGER, OUT out_param INTEGER)
BEGIN
  set @local=100;
  select ほにゃらら;
END
SQL

  def self.down
    execute "DROP PROCEDURE my_procedure;"
  end
end
rake db:migrate

ハマった点

  • DELIMITER は不要。
  • 文末の;は必須
  • selectで出力変数に値を出力する際は、列の末尾に指定する必要がある(?)

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20