在 MySQL 的存储过程中编写可选参数?

2021-11-20 00:00:00 mysql stored-procedures

我想创建一个存储过程,根据传递给它的参数更新表中的所有字段或仅更新其中的几个字段.

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?

推荐答案

Optional Parameters 在 MySQL 上尚不支持.我建议您在参数中传递 null 值,并且在您的存储过程中有一个 IF 语句.

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.

DELIMITER $$
CREATE PROCEDURE procName
(IN param VARCHAR(25))
BEGIN
   IF param IS NULL THEN 
      -- statements ;
   ELSE commands
      -- statements ;
   END IF;
END$$
DELIMITER ;

相关文章