DROP PROCEDURE IF EXISTS `writeoff_day` ;; CREATE PROCEDURE `sumstats_month`() BEGIN DECLARE user_id, tariff_id, b INT; DECLARE tariff_perday DOUBLE; DECLARE cur_u CURSOR FOR SELECT id, tariffid FROM users; DECLARE CONTINUE HANDLER FOR NOT FOUND SET b=1; OPEN cur_u; REPEAT FETCH cur_u INTO user_id, tariff_id; DECLARE cur_t CURSOR FOR SELECT perday FROM tariffs WHERE id=tariff_id; OPEN cur_t; FETCH cur_t INTO tariff_perday; UPDATE users SET balance=balance-tariff_perday WHERE id=user_id; UNTIL b=1 END REPEAT; UPDATE tariffs SET last_writeoff_day=now(); END ;; ERROR 1064 (42000) at line 201: 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 cur_t CURSOR FOR SELECT perday FROM tariffs WHERE id=tariff_id; OPEN c' at line 11