2011-02-04 26 views
0

我想写一个简单的存储过程,这将清除一些表内容在当前数据库 - 通过匹配对表中的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 ; 

我希望能够删除数据库参数,并使该功能在当前活动的数据库上工作。

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

回答

4

考虑到该过程绑定到数据库,当前是被调用的,除非您执行CALL db.proc()

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

where table_schema=database() and table_name like 'cache_%'; 
+0

感谢 - 现在我可以在我建立查询固定我的语法错误工作;) – HorusKol 2011-02-04 00:47:00

相关问题