MySQL 存储过程进行切换表

DELIMITER D R O P P R O C E D U R E I F E X I S T S ‘ s p r e v o k e t a b l e ‘ DROP PROCEDURE IF EXISTS `sp_revoke_table` DROPPROCEDUREIFEXISTS‘spr​evoket​able‘
CREATE DEFINER=root@% PROCEDURE sp_revoke_table(p_table_name VARCHAR(100))
BEGIN
SET @t_tb_src = p_table_name;
SELECT CONCAT("create table _tmpdba like ", @t_tb_src) INTO @t_sql FROM DUAL;
PREPARE stmt_1 FROM @t_sql;
EXECUTE stmt_1;
DEALLOCATE PREPARE stmt_1;

SELECT CONCAT("_", @t_tb_src, "_",  DATE_FORMAT(NOW(),'%Y%m%d_%H%i%s'), "_del") INTO @t_table_today FROM DUAL;
SELECT CONCAT("rename table ", @t_tb_src, " TO ",@t_table_today, ", _tmpdba to ", @t_tb_src ) INTO @t_sql FROM DUAL;
PREPARE stmt_2 FROM @t_sql;
EXECUTE  stmt_2;
DEALLOCATE PREPARE stmt_2;
 
END$$

DELIMITER ;

完整实例:http://github.crmeb.net/u/defu

来自 “开源世界 ” ,链接:http://ym.baisou.ltd/post/620.html,如需转载,请注明出处,否则将追究法律责任。

上一篇:6 张图带你彻底搞懂分布式事务 XA 模式


下一篇:《.NET 编程结构》专题汇总