mysqlstored-proceduresprepared-statementstored-functions

Can I use prepare statement to create function and stored procedures in mysql?


I need to create a new function and stored procedures through the prepare and execute statement. Whether it is possible or not?

prepare statement from 'DELIMITER ;;
CREATE FUNCTION `db_name`.`fn_name`(Params)
RETURNS varchar(50) CHARSET utf8mb4 DETERMINISTIC
BEGIN
DECLARE name varchar(50);
RETURN IFNULL(name,"");
END ;;
DELIMITER ;';
execute statement;

Solution

  • No, it's not possible. See SQL Syntax Permitted in Prepared Statements for the list of statements allowed in prepared statements. CREATE PROCEDURE isn't one of them.