Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How could I run a while loop to update records in my table in phpMyAdmin?

I'm using MySQL, of course.

However, I'm unable to run any while loop in the text box in the browser to update records in a table - I'm getting an error.

DECLARE @count INT
SET @count = 0 
WHILE @count <2000 DO
    /* loop logic in here */
    SET @count = @count + 1;
END WHILE;

Is the above code wrong?

Here is the error: enter image description here

MySQL said:

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use

near 'DECLARE @count INT SET @count = 0 WHILE @count <2000 DO SET @count = @co' at line 1

like image 733
oxo Avatar asked Nov 01 '25 12:11

oxo


1 Answers

You can do this with a procedure like this :

DELIMITER $$
DROP PROCEDURE IF EXISTS test$$
CREATE PROCEDURE test()
BEGIN
 DECLARE count INT DEFAULT 0;
 WHILE count < 2000 DO
   /* statment */
   SET count = count + 1;
 END WHILE;
END$$
DELIMITER ;

mysql>call test();

Hope this help you.

like image 163
Edouard B Avatar answered Nov 03 '25 04:11

Edouard B



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!