Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql while loop Break equivalent

What would be an equivalent of a break in a while loop for mysql?

  WHILE (ctr < i)
  DO ......

    SET cnt = (SELECT COUNT(*) FROM temp_results WHERE result = "true");
    IF cnt > 0 THEN
      SELECT cnt;
      BREAK;
    END IF;

Thanks

like image 327
Shah Avatar asked Nov 14 '11 16:11

Shah


People also ask

How do you break out of a WHILE LOOP in MySQL?

In MySQL, the LEAVE statement is used when you want to exit a block of code identified by a label_name, such as a LOOP statement, WHILE statement, or REPEAT statement.

How do I stop an infinite LOOP in MySQL?

The syntax for this statement is as follows: [ label :] LOOP statements END LOOP [ label ]; The statements between the LOOP and END LOOP statements will be repeated indefinitely, until the LOOP is terminated. You can terminate the LOOP using the LEAVE statement, which we will describe shortly.

Is there WHILE LOOP in MySQL?

MySQL WHILE loop statement is used to execute one or more statements again and again, as long as a condition is true. We can use the loop when we need to execute the task with repetition while condition is true.

What are different kind of loops available in MySQL?

The MySQL stored program language offers three types of loops : Simple loops using the LOOP and END LOOP clauses. Loops that continue while a condition is true, using the WHILE and END WHILE clauses. Loops that continue until a condition is true, using the REPEAT and UNTIL clauses.


2 Answers

got it.

myloop: WHILE (ctr < i)
DO 
   …

   SET cnt = (SELECT COUNT(*) FROM temp_results WHERE result = "true");
   IF cnt > 0 THEN
      SELECT cnt;
      LEAVE myloop;
   END IF;
END WHILE myloop;
like image 107
Shah Avatar answered Sep 22 '22 01:09

Shah


You might be interested in a REPEAT loop:

REPEAT  
    SET cnt = (SELECT COUNT(*) FROM temp_results WHERE result = "true");
UNTIL cnt > 0 
END REPEAT;
like image 38
p.campbell Avatar answered Sep 21 '22 01:09

p.campbell