1

我试图编写 MySQL 程序来删除数据库中的所有数据:

MySQL 服务器版本 5.5.20

看起来像:

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;

结果显示错误:

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

我做错了什么?

4

2 回答 2

1

如果您需要保留结构但只是删除数据,那么只使用结构(-d 选项)执行 mysqldump 会更容易,然后删除数据库并使用 mysqldump 输出重新创建它。

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

mysqlimport -u root -ppassword databasename dumpfile.sql
于 2013-02-22T17:12:39.533 回答
-1

我解决了这个问题,现在可以了:

-- 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 */;
于 2013-02-25T16:54:53.987 回答