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?

3
  • I don't understand why you can't use TRUNCATE Commented Feb 22, 2013 at 16:53
  • Possible duplicate: stackoverflow.com/questions/12403662/drop-all-tables Commented Feb 22, 2013 at 16:53
  • Our structure of DB is not static so we need simple way to remove all data but structure must be stay untouched Commented Feb 25, 2013 at 15:24

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

2 Comments

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. Thanks for pointing that out. Was thinking "import" and confused myself.
-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 */;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.