如何在存储过程中识别当前数据库?

时间:2011-02-04 00:30:22

标签: mysql

我正在尝试编写一个简单的存储过程,它将清除当前数据库中某些表的内容 - 我这样做是通过将一个前缀与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参数并使该函数在当前活动的数据库上运行。

我想我还需要能够识别尚未选择数据库并输出错误(以防止意外刷新所有数据库中的所有缓存表)。

1 个答案:

答案 0 :(得分:4)

考虑到该过程与数据库绑定,除非您执行CALL db.proc(),否则将调用当前流程。

但是,如果你真的想要选择的那个,你可以the DATABASE() function

where table_schema=database() and table_name like 'cache_%';
相关问题