salix/services/db/00-truncateAll.sql

37 lines
874 B
SQL

DROP PROCEDURE IF EXISTS mysql.truncateAll;
DELIMITER $$
CREATE PROCEDURE mysql.truncateAll()
BEGIN
DECLARE vSchema VARCHAR(255);
DECLARE vTable VARCHAR(255);
DECLARE vDone BOOL;
DECLARE cTables CURSOR FOR
SELECT `TABLE_SCHEMA`, `TABLE_NAME`
FROM `information_schema`.`TABLES`
WHERE `TABLE_TYPE` = 'BASE TABLE'
AND `TABLE_SCHEMA` NOT IN ('information_schema', 'mysql', 'performance_schema');
DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE;
SET FOREIGN_KEY_CHECKS = FALSE;
OPEN cTables;
l: LOOP
SET vDone = FALSE;
FETCH cTables INTO vSchema, vTable;
IF vDone THEN
LEAVE l;
END IF;
SET @stmt = CONCAT('TRUNCATE TABLE `', vSchema, '`.`', vTable, '`');
PREPARE stmt FROM @stmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END LOOP;
CLOSE cTables;
SET FOREIGN_KEY_CHECKS = TRUE;
END$$
DELIMITER ;