Is it possible to have a default parameter for a mysql stored procedure?
It's still not possible.
We worked around this limitation by adding a simple IF statement in the stored procedure. Practically we pass an empty string whenever we want to save the default value in the DB.
CREATE DEFINER=`test`@`%` PROCEDURE `myProc`(IN myVarParam VARCHAR(40))
BEGIN
IF myVarParam = '' THEN SET myVarParam = 'default-value'; END IF;
...your code here...
END
SET myParam = IFNULL(myParam, 0);
Explanation: IFNULL(expression_1, expression_2)
The IFNULL
function returns expression_1
if expression_1
is not NULL
; otherwise it returns expression_2
. The IFNULL
function returns a string or a numeric based on the context where it is used.