检查MySQL存储过程中的NULL或空变量
要检查NULL或空变量,请使用IF条件。让我们创建一个存储过程-
delimiter //
create procedure checkingForNullDemo(Name varchar(20))
begin
if Name is NULL OR Name='' then
select 'Adam Smith';
else
select Name;
end if ;
end
//
delimiter ;情况1
传递NULL时。使用调用命令调用存储过程
call checkingForNullDemo(NULL);
这将产生以下输出-
+------------+ | Adam Smith | +------------+ | Adam Smith | +------------+ 1 row in set (0.00 sec)
情况二
传递值时。使用call命令调用存储过程
call checkingForNullDemo('John Doe');这将产生以下输出-
+----------+ | Name | +----------+ | John Doe | +----------+ 1 row in set (0.00 sec)