问题
I can specify that table columns are NOT NULL, but how do I make a stored procedure or function only be compatible with non-null arguments? Adding NOT NULL after the argument name doesn't work.
回答1:
You would need to validate passed parameter values yourself. If you're using MySQL 5.5 and up you can make use of SIGNAL.
DELIMITER //
CREATE PROCEDURE my_procedure (IN param1 INT)
BEGIN
IF param1 IS NULL THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'NULL is not allowed.';
END IF;
-- do whatever
END//
DELIMITER ;
Here is a SQLFiddle demo
来源:https://stackoverflow.com/questions/30607172/disallow-null-parameters-to-stored-procedures-in-mysql-mariadb