CREATE PROCEDURE `proc_copy_table`(IN v_table VARCHAR(20),IN v_db VARCHAR(20),OUT o_result int(4))
BEGIN
DECLARE exit HANDLER FOR SQLEXCEPTION
begin
rollback; -- 有异常,进行回滚
set o_result = -500;
end;
START TRANSACTION;
SET @sql1 = CONCAT('drop table if EXISTS ',v_table,';');
-- drop table if EXISTS v_table;
prepare stmt from @sql1; -- 预处理需要执行的动态SQL,
EXECUTE stmt;
deallocate prepare stmt; -- 释放掉预处理段
set @sql2 = CONCAT('create table ',v_table,' like ',v_db,'.',v_table,';');
-- create table v_table like v_db.v_table;
prepare stmt from @sql2; -- 预处理需要执行的动态SQL,
EXECUTE stmt;
deallocate prepare stmt; -- 释放掉预处理段
set @sql3 = CONCAT('insert into ',v_table,' select * from ',v_db,'.',v_table,';');
-- insert into v_table select * from v_db.v_table;
prepare stmt from @sql3; -- 预处理需要执行的动态SQL,
EXECUTE stmt;
deallocate prepare stmt; -- 释放掉预处理段
COMMIT;
set o_result :=0;
end