1

I have tried to write MySQL procedure to delete all data in the DB:

MySQL Server version 5.5.20

It looks like:

DELIMITER $$
CREATE PROCEDURE drop_data_like(pattern VARCHAR(255), db VARCHAR(255))
begin
    DECLARE truncate_table VARCHAR(255) DEFAULT '';
    DECLARE done INT DEFAULT 0;
    DECLARE cur1 CURSOR FOR SELECT CONCAT(db, '.', info.table_name)
        FROM information_schema.tables AS info
        WHERE info.table_schema=db AND info.table_name LIKE pattern;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

    OPEN cur1;

    read_loop: LOOP
        FETCH cur1 INTO truncate_table;
        IF done THEN LEAVE read_loop; END IF;

        PREPARE stmt FROM truncate_table;

        EXECUTE stmt;
        DROP PREPARE stmt;
    END LOOP;
    CLOSE cur1;
END$$
DELIMITER ;

CALL drop_data_like('%%', DATABASE());

DROP PROCEDURE drop_data_like;

And as the result it has showed error:

ERROR 1064 (42000) at line 16: 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 '@truncate_table;
    IF done THEN LEAVE read_loop; END IF;

    PREPARE ' at line 13

What have I done wrong?

66Ton99
  • 430
  • 6
  • 19

2 Answers2

1

If you need to keep the structure but just get rid of data, then it would be easier to do a mysqldump with structure only (the -d option), then drop the database and recreate it with the mysqldump ouptut.

mysqldump -d -h localhost -u user -ppassword databasename > dumpfile.sql

mysqlimport -u root -ppassword databasename dumpfile.sql
mike
  • 33
  • 5
  • Your `mysqldump` command makes sense, but your import command is wrong. You should do something like this to import the schema: `mysql -u root -ppassword databasename < dumpfile.sql` – Ike Walker Feb 22 '13 at 18:29
  • @Ike. Thanks for pointing that out. Was thinking "import" and confused myself. – mike Feb 22 '13 at 23:50
-1

I solved the problem and this works now:

-- Server version   5.5.20

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;


DELIMITER $$
CREATE PROCEDURE drop_data_like(pattern VARCHAR(255), db VARCHAR(255))
begin
    DECLARE truncate_table VARCHAR(255) DEFAULT '';
    DECLARE done INT DEFAULT 0;
    DECLARE cur1 CURSOR FOR SELECT CONCAT('TRUNCATE TABLE ', db, '.', info.table_name)
        FROM information_schema.tables AS info
        WHERE info.table_schema=db AND info.table_name LIKE pattern;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

    OPEN cur1;

    read_loop: LOOP
        FETCH cur1 INTO truncate_table;
        SET @sql = truncate_table;
        IF done THEN LEAVE read_loop; END IF;
        PREPARE stmt FROM @sql;
        EXECUTE stmt;
        DROP PREPARE stmt;
    END LOOP;
    CLOSE cur1;
END$$
DELIMITER ;

CALL drop_data_like('%%', DATABASE());

DROP PROCEDURE drop_data_like;



/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
66Ton99
  • 430
  • 6
  • 19