要在存储过程中设置条件,请在MySQL中使用IF ... ELSE。以下是if-else的语法-
IF yourCondition then yourStatement1, ELSE yourStatement2, END IF;
让我们在存储过程中实现上述语法-
DELIMITER // CREATE PROCEDURE IF_ELSE_DEMO(IN value int) -> BEGIN -> SET @val=value; -> IF @val > 10 then -> select concat(@val,' is greater than 10'); -> ELSE -> select concat(@val,' is less than 10 '); -> END IF; -> END; -> //
DELIMITER ;
现在您可以使用CALL命令调用存储过程-
call IF_ELSE_DEMO(18);
这将产生以下输出-
+------------------------------------+ | concat(@val,' is greater than 10') | +------------------------------------+ | 18 is greater than 10 | +------------------------------------+ 1 row in set (0.03 sec)