Search code examples
mysqlwhile-loopsyntax-error

while in MYSQL Script not SP or function


I am trying to achieve loop functionality in MySQL script but below throws error.

set @sd = '2021-04-09';
set @ed = '2023-09-27';

WHILE @sd <= @ed DO
    SET @sd = DATE_ADD(@sd, INTERVAL 1 DAY)
END while;

Error Code: 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 'WHILE @sd <= @ed DO SET @sd = DATE_ADD(@sd, INTERVAL 1 DAY) END while' at line 1


Solution

  • You're trying to use MySQL loop / conditional control outside a SP. You Can't Do That™.