I've been researching trying to find a way to fetch all rows from a query result and process them individually. I've written a script which I thought would work but apparently not.
The script:
DECLARE @name char(20);
DECLARE c1 CURSOR READ_ONLY
FOR
SELECT table_name
FROM information_schema.tables WHERE table_schema = 'puslogger' AND UPDATE_TIME < (now() - interval 30 day)
OPEN c1;
FETCH NEXT FROM c1
INTO @table_name
WHILE @@FETCH_STATUS = 0
BEGIN
PREPARE stmt FROM "concat('DROP TABLE IF EXISTS `', @table_name,'`;')"
EXECUTE stmt
DEALLOCTATE stmt
FETCH NEXT FROM c1
INTO @table_name
END
CLOSE c1
DEALLOCATE c1
The script is intended to drop all tables older than 30 days. Although it doesn't seem to work for MySQL Version 5.5.37.
I'm new to MySQL and I'm running a server with MySQL for Windows (XP). Perhaps this syntax for CURSORS isn't correct for the corresponding server version? I'm not sure but I'd be very happy if anyone could help me out.
EDIT:
This is the error message returned when I try to execute the script from the SQL Command Line:
ERROR 1064 (42000): 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 @name char(20)' at line 1
ERROR 1064 (42000): 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 c1 CURSOR READ_ONLY FOR SELECT table_name FROM information_schema.tables' at line 1
ERROR 1064 (42000): 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 'FETCH NEXT FROM c1 INTO @table_name
WHILE @@FETCH_STATUS = 0 BEGIN
PREPARE st' at line 1
UPDATE:
I also tried this script (NOTE: I intend to create a event which will be executed daily to remove tables older than 30 days.):
delimiter |
CREATE EVENT clean_logger
ON SCHEDULE EVERY 1 DAY
DO
BEGIN
DECLARE @name char(20);
DECLARE c1 CURSOR READ_ONLY
FOR
SELECT table_name
FROM information_schema.tables WHERE table_schema = 'puslogger' AND UPDATE_TIME < (now() - interval 30 day);
OPEN c1;
FETCH NEXT FROM c1
INTO @table_name;
WHILE @@FETCH_STATUS = 0
BEGIN
PREPARE stmt FROM "concat('DROP TABLE IF EXISTS `', @table_name,'`;')";
EXECUTE stmt;
DEALLOCTATE stmt;
FETCH NEXT FROM c1
INTO @table_name;
END;
CLOSE c1;
DEALLOCATE c1;
END |
delimiter ;
Running this script in the SQL Command Line returns:
ERROR 1064 (42000): 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 '@name char(20); DECLARE c1 CURSOR READ_ONLY FOR SELECT table_name FROM infor' at line 5