最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

mysql雙游標(biāo)嵌套循環(huán)方式

 更新時(shí)間:2023年07月06日 10:36:46   作者:tonglei111  
這篇文章主要介紹了mysql雙游標(biāo)嵌套循環(huán)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

mysql雙游標(biāo)嵌套循環(huán)

系統(tǒng)要上線新功能,引入了BPMN流程引擎,引入該引擎后就會(huì)涉及到歷史數(shù)據(jù)處理問題,因?yàn)橄到y(tǒng)已經(jīng)生成了上千條業(yè)務(wù)數(shù)據(jù),由于不是靠BPMN引擎流轉(zhuǎn)的,所以需要將業(yè)務(wù)數(shù)據(jù)和流程引擎關(guān)聯(lián)起來,有多少條在執(zhí)行的業(yè)務(wù)數(shù)據(jù),就生成多少條流程實(shí)例,我們的流程引擎使用的是activiti流程引擎框架;

其中一塊的歷史數(shù)據(jù)處理是要查詢出符合條件的歷史數(shù)據(jù),然后遍歷循環(huán)業(yè)務(wù),業(yè)務(wù)數(shù)據(jù)中有個(gè)處理人的字段是多人的,要把這個(gè)多人的以逗號(hào)分隔開,有幾人再次循環(huán)幾次;

廢話不多說,直接上代碼:

delimiter //
drop procedure if exists procedure_test;
create procedure procedure_test()
begin
?? ?declare count_ int default 0;
?? ?declare requirementCount_ int default 1;
?? ?declare assigineeCount_ int default 1;
?? ?declare result_ int default 0;
?? ?declare startUserId_ int ;
?? ?declare runTaskAssignee int ;
?? ?declare businessMainDataId int;
?? ?declare businessCode varchar(10);
?? ?declare users varchar(250);
?? ?declare process_serial_number int default 0;
?? ?-- 創(chuàng)建游標(biāo)1
?? ?declare cursor_test1 cursor for select o.commituserId,o.id,o.code from rp_original_requirement o where o.status = 1 and o.currentNode = 16;
?? ?-- 創(chuàng)建游標(biāo)2 ,這個(gè)游標(biāo)的創(chuàng)建使用了:將數(shù)據(jù)庫內(nèi)某個(gè)字段以逗號(hào)分隔的列,拆成一個(gè)數(shù)據(jù)列表,這樣游標(biāo)2就可以遍歷執(zhí)行這個(gè)數(shù)據(jù)集了
?? ?-- 其中語句:select (id-1) as id from rp_nonstandard_approve c order by id asc limit 21 ?是要查出一個(gè)從0開始的遞增的連續(xù)數(shù)字,rp_nonstandard_approve 的主鍵id剛好是自增的主鍵,limit 21 是因?yàn)閍.approveHandlers字段最多存20個(gè)人,所以這里要大于20;
?? ?declare cursor_test2 cursor for select substring_index(substring_index(a.approveHandlers,',',d.id+1),',',-1) as assignee from rp_nonstandard_approve a join ( select (id-1) as id from rp_nonstandard_approve c order by id asc limit 21 ) d on d.id<(length(a.approveHandlers)-length(replace(a.approveHandlers,',',''))+1) where a.status = 1 and a.requirementID=businessMainDataId ;
?? ?-- 游標(biāo)循環(huán)結(jié)束
?? ?declare exit handler for not found close cursor_test1 ;
?? ?select count(1) into requirementCount_ ?from ?rp_original_requirement o where o.status = 1 and o.currentNode = 16;
?? ?select p.id_ into @procDefId from act_re_prodef p left join act_re_deployment ard on p.deployment_id_= ard.id_
?? ?where p.key_='nonstandardProcess' order by deploy_time_ desc limit 1;
?? ?set @i=1;
?? ?-- 開啟游標(biāo),調(diào)用開啟游標(biāo)時(shí)才會(huì)執(zhí)行對(duì)應(yīng)游標(biāo)的查詢方法
?? ?open cursor_test1 ;
?? ?while @i<=requirementCount_ ?do
?? ??? ?-- 將游標(biāo)中的值賦值給申明的變量,順序要和游標(biāo)定義內(nèi)的一致;
?? ??? ?fetch cursor_test1 into startUserId_ ,businessMainDataId ,businessCode ;
?? ??? ?set process_serial_number = process_serial_number + 1;
?? ??? ?set @processInstanceName = concat('NSP',date_formart(now(),'%Y%m%d%H%i%s'),lpad(process_serial_number,6,0));
?? ??? ?set @mainProcessId=repalce(uuid(),'-','');
?? ??? ?set @startUserId= startUserId_ ;
?? ??? ?-- 運(yùn)行時(shí)實(shí)例
?? ??? ?insert into act_ru_execution (id_, rev_, proc_inst_id_, business_key_, parent_id_, proc_def_id_, super_exec_, root_proc_inst_id_, act_id_, is_active_, is_concurrent_, is_scope_, is_event_scope_, is_mi_root_, suspension_state_, cached_ent_state_, tenant_id_, name_, start_time_, start_user_id_, lock_time_, is_count_enable_, evt_subscr_count_, task_count_, job_count_, timer_job_count_, susp_job_count_, deadletter_job_count_, var_count_, id_link_count_ )
?? ??? ?values (@mainProcessId,1,@mainProcessId,null,null,@procDefId,null,@mainProcessId,null,1,0,1,0,0,1,null,'',@processInstanceName ,sysdate(),@startUserId,null,0,0,0,0,0,0,0,0,0);
?? ?set @middleProcessId = replace(uuid(),'-','');
?? ?set @middleProcessActKey = 'nonStandardApprove';
?? ?insert into act_ru_execution (id_, rev_, proc_inst_id_, business_key_, parent_id_, proc_def_id_, super_exec_, root_proc_inst_id_, act_id_, is_active_, is_concurrent_, is_scope_, is_event_scope_, is_mi_root_, suspension_state_, cached_ent_state_, tenant_id_, name_, start_time_, start_user_id_, lock_time_, is_count_enable_, evt_subscr_count_, task_count_, job_count_, timer_job_count_, susp_job_count_, deadletter_job_count_, var_count_, id_link_count_ )
?? ??? ?values (@middleProcessId,1,@mainProcessId,null,@mainProcessId,@procDefId,null,@mainProcessId,@middleProcessActKey,0,0,0,0,1,1,null,'',null ,sysdate(),null,null,0,0,0,0,0,0,0,0,0);
?? ?-- 當(dāng)前非標(biāo)審批環(huán)節(jié)的處理人數(shù)
?? ?select count(1) into assigineeCount_ ?from (select substring_index(substring_index(a.approveHandlers,',',d.id+1),',',-1) as assignee from rp_nonstandard_approve a join ( select (id-1) as id from rp_nonstandard_approve c order by id asc limit 21 ) d on d.id<(length(a.approveHandlers)-length(replace(a.approveHandlers,',',''))+1) where a.status = 1 and a.requirementID=businessMainDataId ) g;
?? ?select h.approve_handlers into users from rp_nonstandard_approve ?h where h.status = 1 and h.requirementID=businessMainDataId;
?? ?-- 中間運(yùn)行時(shí)變量
?? ?insert into act_ru_variable (id_, rev_, type_, name_, execution_id_, proc_inst_id_, task_id_, bytearray_id_, double_, long_, text_, text2_) values (replace(uuid(),'-',''),1,'interger','nrOfActiveInstances',@middleProcessId,@mainProcessId,null,null,null,assigineeCount_ ?,assigineeCount_ ?,null) ;
?? ?insert into act_ru_variable (id_, rev_, type_, name_, execution_id_, proc_inst_id_, task_id_, bytearray_id_, double_, long_, text_, text2_) values (replace(uuid(),'-',''),1,'interger','nrOfCompletedInstances',@middleProcessId,@mainProcessId,null,null,null,0,0,null) ;
?? ?insert into act_ru_variable (id_, rev_, type_, name_, execution_id_, proc_inst_id_, task_id_, bytearray_id_, double_, long_, text_, text2_) values (replace(uuid(),'-',''),1,'interger','nrOfInstances',@middleProcessId,@mainProcessId,null,null,null,assigineeCount_ ?,assigineeCount_ ?,null) ;
?? ?-- 主運(yùn)行是變量
?? ?insert into act_ru_variable (id_, rev_, type_, name_, execution_id_, proc_inst_id_, task_id_, bytearray_id_, double_, long_, text_, text2_) values (replace(uuid(),'-',''),1,'long','requirementId',@mainProcessId,@mainProcessId,null,null,null,businessMainDataId,businessMainDataId,null) ;
?? ?insert into act_ru_variable (id_, rev_, type_, name_, execution_id_, proc_inst_id_, task_id_, bytearray_id_, double_, long_, text_, text2_) values (replace(uuid(),'-',''),1,'json','users',@mainProcessId,@mainProcessId,null,null,null,null,concat('[',users,']'),null) ;
?? ?-- 歷史實(shí)例
?? ?set @startActId = 'newRequirement';
?? ?insert into act_hi_procinst (id_, proc_inst_id_, business_key_, proc_def_id_, start_time_, end_time_, duration_, start_user_id_, start_act_id_, end_act_id_, super_process_instance_id_, delete_reason_,tenant_id_, name_) values(@mainProcessId,@mainProcessId,null,@procDefId,sysdate(),null,null,@startUserId,@startActId,null,null,null,'',@processInstanceName);
?? ?open cursor_test2 ;
?? ?set @j= 1;
?? ?while @j<=assigineeCount_ ?do
?? ??? ?fetch cursor_test2 ?into runTaskAssignee ;
?? ??? ?-- 根據(jù)非標(biāo)處理人數(shù)循環(huán)插入執(zhí)行數(shù)據(jù)
?? ??? ?set @taskProcessId = replace(uuid(),'-','');
?? ??? ?insert into act_ru_execution (id_, rev_, proc_inst_id_, business_key_, parent_id_, proc_def_id_, super_exec_, root_proc_inst_id_, act_id_, is_active_, is_concurrent_, is_scope_, is_event_scope_, is_mi_root_, suspension_state_, cached_ent_state_, tenant_id_, name_, start_time_, start_user_id_, lock_time_, is_count_enable_, evt_subscr_count_, task_count_, job_count_, timer_job_count_, susp_job_count_, deadletter_job_count_, var_count_, id_link_count_ )
?? ??? ?values (@taskProcessId,1,@mainProcessId,null,@middleProcessId,@procDefId,null,@mainProcessId,@middleProcessActKey,1,0,0,0,0,1,null,'',null ,sysdate(),null,null,0,0,0,0,0,0,0,0,0);
?? ?-- 根據(jù)非標(biāo)處理人數(shù)循環(huán)插入任務(wù)數(shù)據(jù)
?? ?-- 任務(wù)數(shù)據(jù)
?? ?set @runTaskId = replace(uuid(),'-','');
?? ?set @runTaskKey= 'nonStandardApprove';
?? ?set @runTaskName= '審批';
?? ?set @runTaskAssignee = runTaskAssignee;
?? ?insert into act_ru_task (id_,rev_,execution_id_,proc_inst_id_,proc_def_id_,name_,parent_task_id_,description_,task_def_key_,owner_,assignee_,delegation_,priority_,create_time_,due_date_category_,suspension_state_,tenant_id_,form_key_,claim_time_) values (@runTaskId,1,@taskProcessId,@mainProcessId,@procDefId,@runTaskName,NULL,NULL,@runTaskKey,NULL,@runTaskAssignee,NULL,50,sysdate(),null,null,1,'',null,null);
?? ?-- 根據(jù)非標(biāo)處理人數(shù)循環(huán)插入任務(wù)?
?? ?insert into act_ru_variable (id_, rev_, type_, name_, execution_id_, proc_inst_id_, task_id_, bytearray_id_, double_, long_, text_, text2_) values (replace(uuid(),'-',''),1,'string','user',@taskProcessId,@mainProcessId,null,null,null,null,@runTaskAssignee,null) ;
?? ?insert into act_ru_variable (id_, rev_, type_, name_, execution_id_, proc_inst_id_, task_id_, bytearray_id_, double_, long_, text_, text2_) values (replace(uuid(),'-',''),1,'interger','loopCounter',@taskProcessId,@mainProcessId,null,null,null,0,'0',null) ;
?? ?insert into act_ru_variable (id_, rev_, type_, name_, execution_id_, proc_inst_id_, task_id_, bytearray_id_, double_, long_, text_, text2_) values (replace(uuid(),'-',''),1,'string','user',@taskProcessId,@mainProcessId,@runTaskId ,null,null,null,@runTaskAssignee,null) ;?? ?
?? ?insert into act_ru_variable (id_, rev_, type_, name_, execution_id_, proc_inst_id_, task_id_, bytearray_id_, double_, long_, text_, text2_) values (replace(uuid(),'-',''),1,'interger','loopCounter',@taskProcessId,@mainProcessId,@runTaskId,null,null,0,'0',null) ;
?? ?insert into act_ru_variable (id_, rev_, type_, name_, execution_id_, proc_inst_id_, task_id_, bytearray_id_, double_, long_, text_, text2_) values (replace(uuid(),'-',''),1,'interger','nrOfActiveInstances',@taskProcessId,@mainProcessId,@runTaskId,null,null,assigineeCount_ ?,assigineeCount_ ?,null) ;
?? ?insert into act_ru_variable (id_, rev_, type_, name_, execution_id_, proc_inst_id_, task_id_, bytearray_id_, double_, long_, text_, text2_) values (replace(uuid(),'-',''),1,'interger','nrOfCompletedInstances',@taskProcessId,@mainProcessId,@runTaskId,null,null,0,'0',null) ;
?? ?insert into act_ru_variable (id_, rev_, type_, name_, execution_id_, proc_inst_id_, task_id_, bytearray_id_, double_, long_, text_, text2_) values (replace(uuid(),'-',''),1,'interger','nrOfInstances',@taskProcessId,@mainProcessId,@runTaskId,null,null,assigineeCount_ ?,assigineeCount_ ?,null) ;
set @j=@j+1;
end while;
close cursor_test2 ;
set @businessMainDataId = businessMainDataId;
set @businessMainDataTable = 't_original_requirement';
set @businessCode = businessCode ;
insert into rp_pe_business_instance (business_main_data_id,business_main_data_table_name,process_instance_id,version,status,create_by,create_time,update_by,update_by,update_time,is_business_running_process,business_code) values(@businessMainDataId,@businessMainDataTable,@mainProcessId,replace(uuid(),'-',''),1,null,sysdate(),null,sysdate(),1,@businessCode);
set @i=@i+1;
set result_ =result_ +1;
end while;
close cursor_test1 ;
?select concat('數(shù)據(jù)處理?xiàng)l數(shù):',result_ ) as resultdata;
end;

注意事項(xiàng):

游標(biāo)的腳本是在調(diào)用open時(shí)才會(huì)執(zhí)行,比如我的游標(biāo)cursor_test2 中有用到businessMainDataId 變量,但是在聲明游標(biāo)時(shí)這個(gè)變量是沒有值的,fetch cursor_test1 into startUserId_ ,businessMainDataId ,businessCode 方法執(zhí)行時(shí)才會(huì)把這個(gè)變量賦值,之后再執(zhí)行open cursor_test2 時(shí),businessMainDataId 已經(jīng)有了值,這時(shí)就是根據(jù)此時(shí)的businessMainDataId 查詢的審批數(shù)據(jù);

mysql游標(biāo)與嵌套循環(huán)

游標(biāo)嵌套遍歷

user表:

在這里插入圖片描述

CREATE DEFINER=`root`@`localhost` PROCEDURE `test`()
BEGIN
    DECLARE user1 VARCHAR(100);
    DECLARE user2 VARCHAR(100);
    DECLARE done INT DEFAULT 0;
    DECLARE cursor_user1 CURSOR FOR(SELECT `name` FROM `user`); -- 定于游標(biāo)1
    DECLARE cursor_user2 CURSOR FOR(SELECT `name` FROM `user`); -- 定義游標(biāo)2
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1; -- 結(jié)束標(biāo)識(shí)
    OPEN cursor_user1; -- 開啟游標(biāo)1
    loop_1: LOOP -- 外層循環(huán)
    -- -------------------------------------------------------------------------------------
			# 嘗試從游標(biāo)1指向的數(shù)據(jù)集合獲取數(shù)據(jù),如果集合已經(jīng)遍歷完畢,done的值被設(shè)置為1。
			FETCH cursor_user1 INTO user1;
			IF done = 1 THEN LEAVE loop_1; END IF; -- 外層循環(huán)結(jié)束條件
			OPEN cursor_user2; -- 開啟游標(biāo)2
			loop_2: LOOP -- 內(nèi)層循環(huán)
			-- -----------------------------------------------------------------------------
					# 嘗試從游標(biāo)2指向的數(shù)據(jù)集合獲取數(shù)據(jù),如果集合已經(jīng)遍歷完畢,done的值被設(shè)置為1。
					FETCH cursor_user2 INTO user2;
					IF done = 1 THEN LEAVE loop_2; END IF; -- 內(nèi)層循環(huán)結(jié)束條件
					# do something...
		    -- -----------------------------------------------------------------------------
			END LOOP loop_2;
			CLOSE cursor_user2; -- 關(guān)閉游標(biāo)2
			SET done = 0; -- important
    -- ------------------------------------------------------------------------------------
    END LOOP loop_1;
    CLOSE cursor_user1; -- 關(guān)閉游標(biāo)1
END

測(cè)試

user表數(shù)據(jù):

在這里插入圖片描述

CREATE DEFINER=`root`@`localhost` PROCEDURE `test`()
BEGIN
    DECLARE result VARCHAR(2000) DEFAULT 'result';
    DECLARE user1 VARCHAR(100);
    DECLARE user2 VARCHAR(100);
    DECLARE done INT DEFAULT 0;
    DECLARE cursor_user1 CURSOR FOR(SELECT `name` FROM `user`); -- 定于游標(biāo)1
    DECLARE cursor_user2 CURSOR FOR(SELECT `name` FROM `user`); -- 定義游標(biāo)2
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1; -- 結(jié)束標(biāo)識(shí)
    OPEN cursor_user1; -- 開啟游標(biāo)1
    loop_1: LOOP -- 外層循環(huán)
    -- -------------------------------------------------------------------------------------
			# 嘗試從游標(biāo)1指向的數(shù)據(jù)集合獲取數(shù)據(jù),如果集合已經(jīng)遍歷完畢,done的值被設(shè)置為1。
			FETCH cursor_user1 INTO user1;
			IF done = 1 THEN LEAVE loop_1; END IF; -- 外層循環(huán)結(jié)束條件
			OPEN cursor_user2; -- 開啟游標(biāo)2
			loop_2: LOOP -- 內(nèi)層循環(huán)
			-- -----------------------------------------------------------------------------
					# 嘗試從游標(biāo)2指向的數(shù)據(jù)集合獲取數(shù)據(jù),如果集合已經(jīng)遍歷完畢,done的值被設(shè)置為1。
					FETCH cursor_user2 INTO user2;
					IF done = 1 THEN LEAVE loop_2; END IF; -- 內(nèi)層循環(huán)結(jié)束條件
					# do something...
					SET result = CONCAT(result,';',user1,'-',user2);
		    -- -----------------------------------------------------------------------------
			END LOOP loop_2;
			CLOSE cursor_user2; -- 關(guān)閉游標(biāo)2
			SET done = 0; -- important
    -- ------------------------------------------------------------------------------------
    END LOOP loop_1;
    CLOSE cursor_user1; -- 關(guān)閉游標(biāo)1
	SELECT result; 
END

執(zhí)行結(jié)果:

在這里插入圖片描述

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • MySQL隨機(jī)查詢記錄的效率測(cè)試分析

    MySQL隨機(jī)查詢記錄的效率測(cè)試分析

    以下的文章主要介紹的是MySQL使用rand 隨機(jī)查詢記錄效率測(cè)試,我們大家一直都以為MySQL數(shù)據(jù)庫隨機(jī)查詢的幾條數(shù)據(jù),就用以下的東東,其實(shí)其實(shí)際效率是十分低的
    2011-06-06
  • MySQL中的字符串分割函數(shù)及基本用法詳解

    MySQL中的字符串分割函數(shù)及基本用法詳解

    MySQL本身沒有內(nèi)置的SPLIT()函數(shù),但可以通過其他方式實(shí)現(xiàn)字符串分割功能,下面給大家介紹MySQL中的字符串分割函數(shù)及基本用法,感興趣的朋友一起看看吧
    2025-07-07
  • MySQL如何插入Emoji表情

    MySQL如何插入Emoji表情

    這篇文章主要介紹了MySQL如何插入Emoji表情,幫助大家更好的理解和使用MySQL,感興趣的朋友可以了解下
    2020-12-12
  • MySQL中使用序列Sequence的方式總結(jié)

    MySQL中使用序列Sequence的方式總結(jié)

    序列是一組整數(shù)如1,2,3,...為了在需要時(shí)生成的,這篇文章主要給大家介紹了關(guān)于MySQL中使用序列Sequence的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-09-09
  • MySQL的安裝以及基本的管理命令和設(shè)置

    MySQL的安裝以及基本的管理命令和設(shè)置

    這篇文章主要介紹了MySQL的安裝以及基本的管理命令和設(shè)置,是搭建MySQL環(huán)境的基礎(chǔ),需要的朋友可以參考下
    2015-11-11
  • mysql varchar類型求和實(shí)例操作

    mysql varchar類型求和實(shí)例操作

    在本文里我們給大家分享了關(guān)于mysql varchar類型求和實(shí)例操作以及相關(guān)知識(shí)點(diǎn),需要的朋友們學(xué)習(xí)參考下。
    2019-03-03
  • Linux中部署MySQL環(huán)境的四種方式圖文詳解

    Linux中部署MySQL環(huán)境的四種方式圖文詳解

    這篇文章主要介紹了Linux中部署MySQL環(huán)境的四種方式,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2023-11-11
  • mysql CPU高負(fù)載問題排查

    mysql CPU高負(fù)載問題排查

    這篇文章主要介紹了mysql CPU高負(fù)載問題排查的相關(guān)資料,幫助大家更好的理解和使用MySQL,維護(hù)數(shù)據(jù)庫,感興趣的朋友可以了解下
    2020-11-11
  • 淺談為什么MySQL不建議delete刪除數(shù)據(jù)

    淺談為什么MySQL不建議delete刪除數(shù)據(jù)

    這篇文章主要介紹了淺談為什么MySQL不建議delete刪除數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • Mysql使用索引的正確方法及索引原理詳解

    Mysql使用索引的正確方法及索引原理詳解

    這篇文章主要給大家介紹了關(guān)于Mysql使用索引的正確方法及索引原理的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用mysql具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-05-05

最新評(píng)論

开封县| 汝州市| 安康市| 彝良县| 腾冲县| 宁波市| 宁乡县| 云南省| 井研县| 建平县| 枝江市| 三亚市| 香港 | 遂平县| 内江市| 民丰县| 察雅县| 册亨县| 长武县| 安多县| 凤山县| 德化县| 林芝县| 定远县| 盐山县| 弥勒县| 明光市| 平定县| 石渠县| 大方县| 芜湖县| 辽阳县| 大渡口区| 达拉特旗| 彩票| 大关县| 澜沧| 建德市| 如皋市| 佳木斯市| 宜昌市|