老项目有存储过程,每次发版更新,备份原来的存储过程代码,下面的方法很方便的实现
1. 创建存储过程
CREATE DEFINER=`ccstest`@`%` PROCEDURE `my_prop_make_bak_sql`(psShecmal varchar(30),psFunctions varchar(3000))
BEGIN
select CONCAT('-- 备份 ',name,' \r\nDELIMITER $$\r\n \r\nCREATE OR REPLACE ',type,' ',name,'(',CAST(param_list AS CHAR(50000) CHARACTER SET utf8),') ' ,if(type='FUNCTION',' RETURNS ',''),if(type='FUNCTION',CAST(`RETURNS` AS CHAR(50000) CHARACTER SET utf8),''),' \r\n',CAST(body AS CHAR(500000) CHARACTER SET utf8),'$$\r\n \r\nDELIMITER ;\r\n \r\n') tt
from mysql.proc where db = psShecmal and FIND_IN_SET(LOWER(name),LOWER(psFunctions))>0;
END
2. 调用
-- 第一个参数 schema
-- 第二个参数 要备份的存储过程名称, 多个用英文逗号分隔
call my_prop_make_bak_sql('schema', 'propnam1, propname2')