Disallow NULL parameters to stored procedures in MySQL/MariaDB

杀马特。学长 韩版系。学妹 提交于 2019-12-24 13:04:24

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!