MySQL stored procedure loop clears data table

In the case of sub-tables, the data table is cyclically cleared.
Suppose there are tables student0 - student99.
Wouldn’t it be useless to manually clear them?
insert image description here

1. View stored procedures

SHOW PROCEDURE STATUS;

2. Use the stored procedure loop to clear the data table

DROP PROCEDURE if exists truncateTable;
delimiter $$
CREATE PROCEDURE truncateTable()
BEGIN
	DECLARE i INT DEFAULT 0;
	while i <= 99
		do
			SET @tablename = CONCAT("student", i);
			SET @sqlstring = CONCAT("truncate table ", @tablename);
			PREPARE stmt FROM @sqlstring;
			EXECUTE stmt;
			SET i = i + 1;
	END while;
END$$
delimiter ;

CALL truncateTable();
DROP PROCEDURE truncateTable;

Guess you like

Origin blog.csdn.net/sdujava2011/article/details/129884092