继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

使用存储过程批量迁移数据

judyW
关注TA
已关注
手记 47
粉丝 11
获赞 107
-- --------------------------------------------------------
-- 主机:                                  xxx             
-- 服务器版本:                        5.7.20-log - Source distribution
-- 服务器操作系统:                      Linux
-- HeidiSQL 版本:                  9.5.0.5291
-- --------------------------------------------------------

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET NAMES utf8 */;
/*!50503 SET NAMES utf8mb4 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;

-- 导出  过程 xxx 结构
DELIMITER //
CREATE DEFINER=`ai_call_engine`@`%` PROCEDURE `update_call_detail_batch`(
	IN `startId` BIGINT,
	IN `endId` BIGINT
)
    COMMENT 'robot_call_job_id,dialog_flow_id,start_time'
BEGIN
	-- 定义变量
	DECLARE s int DEFAULT 0;
	
	DECLARE robotCallJobId BIGINT(20);
	DECLARE dialogFlowId BIGINT(20);
	DECLARE startTime TIMESTAMP;
	DECLARE callRecordId BIGINT(20);
	
	-- 定义游标,并将sql结果集赋值到游标中
	DECLARE report CURSOR FOR select robot_call_job_id,dialog_flow_id,start_time,call_record_id  from call_record where call_record_id > startId and call_record_id < endId;
	-- 声明当游标遍历完后将标志变量置成某个值
	DECLARE CONTINUE HANDLER FOR NOT FOUND SET s=1;
	-- 打开游标
	open report;
		-- 将游标中的值赋值给变量,注意:变量名不要和返回的列名同名,变量顺序要和sql结果列的顺序一致
		fetch report into robotCallJobId,dialogFlowId,startTime,callRecordId;
		-- 当s不等于1,也就是未遍历完时,会一直循环
		while s<>1 do
			-- 执行业务逻辑
			update call_detail set robot_call_job_id = robotCallJobId,dialog_flow_id = dialogFlowId,start_time = startTime  where call_record_id=callRecordId;
			-- 将游标中的值再赋值给变量,供下次循环使用
			fetch report into robotCallJobId,dialogFlowId,startTime,callRecordId;
		-- 当s等于1时表明遍历以完成,退出循环
		end while;
	-- 关闭游标
	close report;
END//
DELIMITER ;

/*!40101 SET SQL_MODE=IFNULL(@OLD_SQL_MODE, '') */;
/*!40014 SET FOREIGN_KEY_CHECKS=IF(@OLD_FOREIGN_KEY_CHECKS IS NULL, 1, @OLD_FOREIGN_KEY_CHECKS) */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;

打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP