首页 > 代码库 > 使用存储过程---将关联企业信息表中,客户的主版本信息拷贝到副版本中
使用存储过程---将关联企业信息表中,客户的主版本信息拷贝到副版本中
DROP PROCEDURE IF EXISTS `into_relation`;
DELETE FROM crm_relation WHERE client_ID IN (SELECT c.`client_ID` FROM crm_client c WHERE c.`isMainVersion` = 0);
DELIMITER $
CREATE PROCEDURE `into_relation`() COMMENT ‘插入关联企业信息‘
BEGIN
-- 声明变量
DECLARE v_client_Id INT;
DECLARE v_count INT;
DECLARE s1 INT;
/** 声明游标,并将查询结果存到游标中 **/
DECLARE client_Id_cursor CURSOR FOR
SELECT client_Id FROM crm_client c WHERE c.`clientGUID` IN (SELECT DISTINCT c.`clientGUID` FROM crm_relation b LEFT JOIN crm_client c ON c.`client_ID`=b.`client_ID`) AND c.`isMainVersion`=0 ORDER BY client_ID;
SELECT COUNT(client_Id) INTO v_count FROM crm_client c WHERE c.`clientGUID` IN (SELECT DISTINCT c.`clientGUID` FROM crm_relation b LEFT JOIN crm_client c ON c.`client_ID`=b.`client_ID`) AND c.`isMainVersion`=0 ORDER BY client_ID;
SET s1 = 1;
-- 开始事务
START TRANSACTION;
-- 打开游标
OPEN client_Id_cursor;
-- 循环游标
WHILE (s1 < v_count+1) DO
-- 遍历游标
FETCH client_Id_cursor INTO v_client_Id;
-- 嵌套使用游标
BEGIN
DECLARE v_relationClientID INT(11);
DECLARE v_relationDesc VARCHAR(200);
DECLARE v_createUserName VARCHAR(20);
DECLARE v_createDateTime DATETIME;
DECLARE v_updateUserName VARCHAR(20);
DECLARE v_updateDateTime DATETIME;
DECLARE v_count2 INT;
DECLARE s2 INT;
DECLARE relation_cursor CURSOR FOR
SELECT b.`relationClientID`,b.`relationDesc`,b.`createUserName`,b.`createDateTime`,b.`updateUserName`,b.`updateDateTime` FROM crm_relation b
WHERE b.`client_ID`=(SELECT c.`client_ID` FROM crm_client c WHERE c.`clientGUID` = (SELECT c2.`clientGUID` FROM crm_client c2 WHERE c2.`client_ID` = v_client_Id) AND c.`isMainVersion`=1) ORDER BY relation_id;
SET v_count2 = 0;
SELECT COUNT(b.`relation_ID`) INTO v_count2 FROM crm_relation b
WHERE b.`client_ID`=(SELECT c.`client_ID` FROM crm_client c WHERE c.`clientGUID` = (SELECT c2.`clientGUID` FROM crm_client c2 WHERE c2.`client_ID` = v_client_Id) AND c.`isMainVersion`=1) ORDER BY relation_id;
SET s2 = 1;
OPEN relation_cursor;
WHILE (s2 < v_count2+1) DO
FETCH relation_cursor INTO v_relationClientID,v_relationDesc,v_createUserName,v_createDateTime,v_updateUserName,v_updateDateTime;
INSERT INTO crm_relation (`client_ID`,`relationClientID`,`relationDesc`,`createUserName`,`createDateTime`,`updateUserName`,`updateDateTime`)
VALUES (v_client_Id,v_relationClientID,v_relationDesc,v_createUserName,v_createDateTime,v_updateUserName,v_updateDateTime);
SET s2 = s2 + 1;
END WHILE;
CLOSE relation_cursor;
END;
SET s1 = s1 + 1;
END WHILE;
CLOSE client_Id_cursor;
COMMIT; -- 事务提交
END$
DELIMITER ;
CALL into_relation();
/**
创建存储过程: create procedure ‘into_relation()‘ comment ‘插入关联企业信息‘
begin :
--声明要使用到的变量---
declare v_client_id int;
declare v_count int;
declare s1 int;
--声明游标,并将查询结果存到游标中(client_id)--
declare client_id_cursor cursor for (数据源)
select cleint_id from crm_client c where c.client_id in (select distinct
c.clientGUID from crm_relation b left join crm_client c on c.client_id = b.client_id)
and c.isMainVersion = 0 order by client_id
--将查询出来的client_id 存放在游标中--
select count(client_id) into v_vount from ------计算游标长度,即存放数据的个数;
set s1 = 1;
--开始事务---
start transaction;
--打开游标---
open client_id_cursor;
--使用while循环控制 循环游标--
while (s1<v_count+1) DO
--遍历游标--
fetch client_id_cursor into v_client_id;
--嵌套使用游标--
--又一次begin--
declare v_relationClientID INT (11);
declare v_relationDesc varchar(200);
declare v_count2 int;
declare s2 int;
declare relation_cursor cursor for(数据源)
----查询数据--
select b.relationClientID ,b.relationDesc,b.createUserName
from crm_relation b
where b.client_id = (select c.client_id from crm_client c where c.clientGUID =
(select c2.clientGUID FROM crm_client c2 where c2.client_id = v_client_id(此id为每一循环时从游标1中去除的id))
and c.isMainVersion = 1 ) order by relation_id;
set v_count2 = 0;
set count(b.relation_id) into v_count2 from crm_relation b
where b.client_id=........
set s2 = 1;
--打开第二个游标
open relation_cursor;
*/
使用存储过程---将关联企业信息表中,客户的主版本信息拷贝到副版本中