Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disallow NULL parameters to stored procedures in MySQL/MariaDB

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.

like image 291
Display Name Avatar asked Oct 22 '25 03:10

Display Name


1 Answers

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

like image 189
peterm Avatar answered Oct 24 '25 07:10

peterm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!