Ashok Gupta
Ashok Gupta

Reputation: 2297

Condional looping in mysql

Inside MySQL I have written a Procedure where I want to run a loop like this:

While (Cond) Do
  ...(Body1)
  ...
  If (Condition2)
    continue ;

  ...(Body2)
  ...
end while ;

Under the while loop I want the full body to run in case where Condition2 is not met (ie Body1 and Body2).

Currently, when Condition 2 is met, it just executes Body1 and then continues(Check Cond in While and Continue looping.)

Can someone help with the proper syntax to perform the above?

Upvotes: 0

Views: 261

Answers (1)

Quassnoi
Quassnoi

Reputation: 425843

BEGIN
    WHILE cond1 DO
        CALL body1;
        IF (NOT cond2) THEN
            CALL body2;
        END IF;
    END WHILE;
END;

Upvotes: 1

Related Questions