BEGIN
declare startDate VARCHAR(50);
declare regionName VARCHAR(200);
declare companyName VARCHAR(200);
declare departmentName VARCHAR(200);
declare pos VARCHAR(200);
declare regionId bigint;
declare userId bigint;
declare oldId bigint;it
#定義遊標結束
declare stop int default 0;io
#定義遊標ast
declare cur CURSOR FOR (SELECT DATE_FORMAT(u.startDate,'%Y-%m-%d %H:%i:%s') startDate ,u.region_id regionId , u.id userId,
r.`name` regionName,c.`name` companyName,d.`name` departmentName,u.pos pos
FROM `ck_user` u
INNER JOIN ck_user_region r on r.id=u.region_id
left JOIN ck_user_company c on c.id=u.company_id
left JOIN ck_user_department d on d.id=u.department_id
where u.region_id is not null and u.startDate is not null);循環
#設置結束
declare CONTINUE HANDLER FOR SQLSTATE '02000' SET stop=1; im
DELETE FROM ck_user_growth_recorditem;
DELETE FROM ck_user_growth_record;top
#打開遊標
OPEN cur;
FETCH cur INTO startDate,regionId,userId,regionName,companyName,departmentName,pos;
WHILE stop <> 1 DO
#循環
INSERT into ck_user_growth_record(createTime,creater_id,region_id)
VALUES(startDate,userId,regionId);
set oldId=last_insert_id();di
INSERT into ck_user_growth_recorditem(content,createTime,growthType,creater_id,userGrowthRecord_id)
VALUES(regionName,startDate,1,userId,oldId);
if companyName is not null &&companyName!=''then
INSERT into ck_user_growth_recorditem(content,createTime,growthType,creater_id,userGrowthRecord_id)
VALUES(companyName,startDate,2,userId,oldId);co
end if;
if departmentName is not null &&departmentName!=''then
INSERT into ck_user_growth_recorditem(content,createTime,growthType,creater_id,userGrowthRecord_id)
VALUES(departmentName,startDate,3,userId,oldId);tar
end if;
if pos is not null &&pos!=''then
INSERT into ck_user_growth_recorditem(content,createTime,growthType,creater_id,userGrowthRecord_id)
VALUES(pos,startDate,6,userId,oldId);
end if;
#下一個 FETCH cur INTO startDate,regionId,userId,regionName,companyName,departmentName,pos; END WHILE; CLOSE cur; END