2

The stored procedure looks something like:

CREATE PROCEDURE `manager`(IN amount decimal(9,2), IN list text, IN acc_id int(11), OUT return_code int(1))
BEGIN

 DECLARE exit handler for sqlexception, sqlwarning
  BEGIN
  set return_code = 1;
  rollback;
  END;
  SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
  --my code
  set return_code = 0;
END

The method from rails model is:

return_code = -1
return_code = self.connection.execute("call manager(#{amount}, '#{list}', #{acc_id}, #{return_code})")

But this throws an error:

ActiveRecord::StatementInvalid: Mysql2::Error: OUT or INOUT argument 4 for routine staging.manager is not a variable or NEW pseudo-variable in BEFORE trigger: call manager(2222, 'list', 2, -1)

Need some help on how to pass OUT parameter while calling stored procedure from rails.

codevj
  • 103
  • 1
  • 14

1 Answers1

0

The solution below worked for me:

I just changed the #{return_code) with @return_code (no need to pass return_code = -1 as input, as it is an OUT parameter):

return_code = self.connection.execute("call manager(#{amount}, '#{list}', #{acc_id}, @return_code)")

In the stored procedure, I replaced return_code with @return_code and added a select statement in the end:

CREATE PROCEDURE `manager`(IN amount decimal(9,2), IN list text, IN acc_id int(11), OUT return_code int(1))
BEGIN

 DECLARE exit handler for sqlexception, sqlwarning
 BEGIN
  set return_code = 1;
  rollback;
 END;
 SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
   --my code
 set return_code = 0;
 SELECT @return_code;
END
codevj
  • 103
  • 1
  • 14