This question already has an answer here:
这个问题在这里已有答案:
- Create a function with optional arguments in MySQL 3 answers
- 在MySQL 3答案中创建一个带有可选参数的函数
I would like to create a stored procedure which updates either all fields in a table or just a few of them according to parameters passed to it.
我想创建一个存储过程,根据传递给它的参数更新表中的所有字段或只更新其中的一些字段。
How do I create a stored procedure that accepts optional parameters?
如何创建接受可选参数的存储过程?
2 个解决方案
#1
37
Optional Parameters
are not yet supported on MySQL. I'm suggesting that you pass null
value in your parameter and inside your stored procedure has an IF
statement.
MySQL尚不支持可选参数。我建议您在参数中传递null值,并在存储过程内部有一个IF语句。
DELIMITER $$
CREATE PROCEDURE procName
(IN param VARCHAR(25))
BEGIN
IF param IS NULL THEN
-- statements ;
ELSE commands
-- statements ;
END IF;
END$$
DELIMITER ;
#2
5
An special case is when the parameter cant' be NULL, i.e. because is a key. I use a trick for these case: I set the parameter to -1:
一个特例是当参数'不能为NULL时,即因为是一个键。我在这种情况下使用了一个技巧:我将参数设置为-1:
CREATE PROCEDURE procCreate
(IN id_cosa INT(11))
BEGIN
IF id_cosa != -1 THEN
~~(your code here)~~
END IF
END
#1
37
Optional Parameters
are not yet supported on MySQL. I'm suggesting that you pass null
value in your parameter and inside your stored procedure has an IF
statement.
MySQL尚不支持可选参数。我建议您在参数中传递null值,并在存储过程内部有一个IF语句。
DELIMITER $$
CREATE PROCEDURE procName
(IN param VARCHAR(25))
BEGIN
IF param IS NULL THEN
-- statements ;
ELSE commands
-- statements ;
END IF;
END$$
DELIMITER ;
#2
5
An special case is when the parameter cant' be NULL, i.e. because is a key. I use a trick for these case: I set the parameter to -1:
一个特例是当参数'不能为NULL时,即因为是一个键。我在这种情况下使用了一个技巧:我将参数设置为-1:
CREATE PROCEDURE procCreate
(IN id_cosa INT(11))
BEGIN
IF id_cosa != -1 THEN
~~(your code here)~~
END IF
END