Writing additional parameters in stored procedures in MySQL?

I would like to create a stored procedure that updates either all the fields in the table, or several of them in accordance with the parameters passed to it.

How to create a stored procedure that accepts optional parameters?

+27
mysql stored-procedures
Sep 29 '12 at 11:18
source share
2 answers

Optional Parameters are not yet supported in MySQL. I suggest you pass a null value in your parameter and inside your stored procedure has an IF .

 DELIMITER $$ CREATE PROCEDURE procName (IN param VARCHAR(25)) BEGIN IF param IS NULL THEN -- statements ; ELSE commands -- statements ; END IF; END$$ DELIMITER ; 
+34
Sep 29 '12 at 11:22
source share

A special case is when the cant 'parameter is NULL, i.e. is the key. I use the trick for this case: I set the parameter to -1:

 CREATE PROCEDURE procCreate (IN id_cosa INT(11)) BEGIN IF id_cosa != -1 THEN ~~(your code here)~~ END IF END 
+5
Feb 22 '14 at 2:02 on
source share



All Articles