CREATE DEFINER=`root`@`localhost` PROCEDURE `update_proces_init_idx_rec__while1`()
BEGIN
	-- update `CRM_PROCES_idx_TEMP` as i set i.`idx_PROCES_INIT_ID`=IF(i.`TYPE`='PROCES_INIT', i.`ID_PROCES`, 0);
	-- P_INIT_ID.1--fill_idx_INIT
	SET @i = 0;
	SET @loopLomit = 100;
	SET @pinitCnt = 1;
	WHILE @i < @loopLomit and @pinitCnt > 0 DO
		update `CRM_PROCES_idx_TEMP` p
			join `CRM_PROCES_idx_TEMP` pp on ( pp.`ID_PROCES` = p.`PARENT_ID` )
			-- set p.`idx_MAIN_PROCES_INIT_ID`=pp.`idx_MAIN_PROCES_INIT_ID`
			-- where p.`idx_MAIN_PROCES_INIT_ID`=0 and pp.`idx_MAIN_PROCES_INIT_ID`>0
			set p.`idx_PROCES_INIT_ID` = IF( p.`idx_PROCES_INIT_ID` > 0, p.`idx_PROCES_INIT_ID`, pp.`idx_PROCES_INIT_ID` )
				, p.`idx_MAIN_PROCES_INIT_ID` = pp.`idx_MAIN_PROCES_INIT_ID`
			-- // TODO:?: update here `idx_MAIN_PROCES_INIT_ID`
			where p.`idx_PROCES_INIT_ID` = 0 and pp.`idx_PROCES_INIT_ID` > 0
		;
		SET @pinitCnt = ROW_COUNT();
		SET @i = @i + 1;
	END WHILE;
END
