I want to write IF
statement inside a Stored procedure
in MySQL in the following way:
IF (exp1 and exp2 and exp3) or exp4
I know MySQL will treat IF()
as a function call. But I hope you got what I'm trying to achieve. I am new to MySQL syntax.
The syntax for the IF-THEN-ELSE statement in MySQL is: IF condition1 THEN {... statements to execute when condition1 is TRUE...} [ ELSEIF condition2 THEN {...
When you combine each one of them with an IF statement, they read like this: AND – =IF(AND(Something is True, Something else is True), Value if True, Value if False) OR – =IF(OR(Something is True, Something else is True), Value if True, Value if False) NOT – =IF(NOT(Something is True), Value if True, Value if False)
As with other flow-control constructs, IF ... END IF blocks may be nested within other flow-control constructs, including other IF statements. Each IF must be terminated by its own END IF followed by a semicolon.
In a procedure the use of an IF
is pretty straight forward:
IF (yourCondition [logical operator(OR, AND) another condition] ) THEN
So in a practical example:
.... DECLARE m integer; DECLARE n integer; SET m = 1; SET n = 0; IF ((m>n AND m=1 AND n=0) OR m=n)THEN some code here END IF;
The evaluation of the conditions follows the parenthesis rule same as in a mathematical operation.
You can refer to the Docs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With