这是我使用的this答案:
SET @pattern = '%_movielist';
SELECT concat('TRUNCATE TABLE ', GROUP_CONCAT(concat(TABLE_NAME)), ';')
INTO @truncatelike FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME LIKE @pattern;
SELECT @truncatelike;
PREPARE stmt FROM @truncatelike;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
但我收到此错误Access denied for user 'root'@'%' to database 'information_schema'
。
我究竟做错了什么?它似乎适用于其他用户
答案 0 :(得分:2)
您尝试在" information_schema" 数据库上执行此语句。阅读有关此数据库[https://dev.mysql.com/doc/refman/5.7/en/information-schema.html]
的更多信息您不应该在information_schema数据库上运行语句(除非您真的知道自己在做什么)。该数据库充当了一个" meta"指示服务器如何运行的存储库。有可能你没有必要触摸它,如果你这样做,你很可能会破坏你的服务器。
这已在这里得到解答。 [#1044 - Access denied for user 'root'@'localhost' to database 'information_schema'
对上面的限制:只有当语句返回的表的数量超过1个表时,此查询才有效,您需要在迭代中使用它。
为了使所有匹配模式的表能够使用存储过程。
请更改程序名称
CREATE PROCEDURE `new_procedure`()
BEGIN
-- Pattern to Match
SET @pattern = '%_movielist';
-- Temporary Table to Store the Result of The Select Statement
CREATE TEMPORARY TABLE IF NOT EXISTS Table_ToBeTruncated
(
Id int NOT NULL AUTO_INCREMENT,TableName varchar(100),
PRIMARY KEY (id)
);
-- Insert all the TableName to be Truncated
insert Table_ToBeTruncated(TableName)
SELECT distinct concat('TRUNCATE TABLE `', TABLE_NAME, '`;')
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME LIKE @pattern and TABLE_SCHEMA = 'movielist';
-- Declare a variable to count the no of records to be truncated.
SET @count=(Select count(*)from Table_ToBeTruncated);
-- Iterate the list
WHILE @count> 0 DO
-- Pick One table from the Temporary Table List;
SELECT TableName into @truncatelike from Table_ToBeTruncated where ID= @count;
-- Prepare the statement
PREPARE stmt FROM @truncatelike;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
-- Decrease the counter.
set @count = @count- 1;
END WHILE;
drop TEMPORARY TABLE IF EXISTS Table_ToBeTruncated ;
END