我正在尝试编写一个简单的存储过程,它将清除当前数据库中一些 表的内容 - 我通过将前缀与 information_schema 中的表列表进行匹配来实现:
delimiter $$
create procedure local_flush_cache(db varchar(255))
begin
select @str_sql:=concat('delete from ',group_concat(table_name))
from information_schema.tables
where table_schema=db and table_name like 'cache_%';
prepare stmt from @str_sql;
execute stmt;
drop prepare stmt;
end$$
delimiter ;
我希望能够删除 db 参数并让该函数在当前事件的数据库上运行。
我想我还需要能够识别尚未选择数据库并输出错误(以防止意外刷新所有数据库中的所有缓存表)。
最佳答案
考虑到该过程与数据库相关联,除非您执行 CALL db.proc()
,否则将调用当前过程。
但是,如果你真的想要选择的那个,你可以the DATABASE()
function :
where table_schema=database() and table_name like 'cache_%';
关于mysql - 如何在存储过程中识别当前数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4893368/