在MySQL/MariaDB中不允许对存储过程使用NULL参数 [英] Disallow NULL parameters to stored procedures in MySQL/MariaDB
本文介绍了在MySQL/MariaDB中不允许对存储过程使用NULL参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以指定表列不是NULL,但是如何使存储过程或函数仅与非空参数兼容?在参数名称后添加NOT NULL不起作用.
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.
推荐答案
您需要亲自验证传递的参数值.如果您使用的是MySQL 5.5及更高版本,则可以使用 SIGNAL
.
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 ;
这是一个 SQLFiddle 演示
Here is a SQLFiddle demo
这篇关于在MySQL/MariaDB中不允许对存储过程使用NULL参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文