为此,请使用COALESCE()。让我们实现一个存储过程来检查局部变量是否为null-
mysql> DELIMITER //
mysql> CREATE PROCEDURE local_VariableDemo()
BEGIN
DECLARE value1 int;
DECLARE value2 int;
select value1,value2;
select
concat('After checking local variable is null the sum is = ',COALESCE(value1,0)+COALESCE(value2,0));
END
//
mysql> DELIMITER ;使用CALL命令调用存储过程-
mysql> call local_VariableDemo();
这将产生以下输出-
+--------+--------+
| value1 | value2 |
+--------+--------+
| NULL | NULL |
+--------+--------+
1 row in set (0.00 sec)
+-----------------------------------------------------------------------------------------------------+
| concat('After checking local variable is null the sum is = ',COALESCE(value1,0)+COALESCE(value2,0)) |
+-----------------------------------------------------------------------------------------------------+
| After checking local variable is null the sum is = 0 |
+-----------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)