我试图编写 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
我做错了什么?