Question

I was writing a stored procedure in mysql and was meddling with the out parameter. When I specify the parameter in the definition of the SP, I get an error while executing stating that there are N+1 parameters while only N parameters are available. And if i mention any value for the out parameter then I get no results.

How do I get the parameters passed?

Also is it a good practice to just select the values/columns and handle things on the app layer?

Please suggest. I am a newbie for DB (at least good in programming) and not very clear on these concepts.

Thanks in advance for all the time and efforts.

Was it helpful?

Solution

OUT parameters and "results" from stored procedures are two unrelated things.

If you want one or more result sets returned to a client by a stored procedure, you simply use a SELECT statement in the body of the procedure, and whatever you SELECT, whether it's a variable, or a string or numeric literal, is returned to the client.

SELECT NOW() AS the_current_time;  -- do this inside the stored procedure

This will return the current time from the system clock to the client calling the stored procedure, as a "column" called "the_current_time" ...

OUT parameters declare variables within the procedure, whose value is set back into the variable you used in that position when you called the procedure, when the procedure finishes.

DELIMITER $$
CREATE PROCEDURE get_the_time (OUT foo DATETIME) 
BEGIN
  SET foo = NOW();
END $$
DELIMITER ;

...then, call the procedure with a user-defined variable.

CALL get_the_time(@bar);

Subsequently, you'll find that the value that existed inside the procedure in the variable "foo" has been assigned to the variable @bar.

SELECT @bar;

To capture output parameter values, you have to fill the placeholders with variables when you call the procedure, which is why you're getting the N+1 error... you aren't filling the placeholders.

The difference between IN, OUT, and INOUT variables is this:

An IN variable, if passed in from a user defined variable or a stored program variable (when calling one stored procedure from another stored program) does not modify the original value. For IN parameters, you can also use literals as arguments.

An OUT variable returns the last value from the variable inside the procedure, to the variable used in that positional slot when the procedure is called. Inside the procedure, the former value of the variable is not available.

An INOUT variable passes a value in, and the final value of the internal variable gets set back into the external variable.

Since OUT and INOUT can change the value of the variable used to call the procedure, you can't use literals in those slots, when calling a procedure, since, obviously enough, the value of a literal can never be modified within the body of the procedure.

If IN, OUT, or INOUT isn't specified, IN is assumed.

OTHER TIPS

I don't really understand why you use an out parameter. Are you writing a function, and in this case there's no out parameter, just a return statement, or a procedure, and you should not use a procedure to modify and/or return a value ?

For your second question, it depends. If you make a lot of treatments inside your base (with stored procedures and triggers), you'll have less things to do in application side, but you'll have a heavy base, with long timeouts. Especially with a DBMS like MySQL, which isn't all that powerfull, sadly ... Remember, it always depends of your needs. But I'll suggest that you only select values inside your base, and make all the treatments application-side. In your database, only use triggers (when needed, not always) to check your datas and for updating processes, but still remember, it makes your database heavier and your queries longer. And there's another thing you must ask yourself before : is this project always gonna stay like this ? Remember that MySQL stored procedures, Oracle SP, PostGres SP, ... aren't always compatible.

(sorry for my poor english, I'm french)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top