mySQL procedure does not commit on commit statement - mysql

This is my code:
BEGIN
DECLARE _cnt INT;
DECLARE i INT DEFAULT 0 ;
DECLARE j INT DEFAULT 0 ;
DECLARE _FIPSAPNCombo VARCHAR(30) DEFAULT '00000';
DECLARE _prv_FIPSAPNCombo VARCHAR(30) DEFAULT '00000';
DECLARE _Rollyear INT DEFAULT 2000;
DECLARE _first_Rollyear INT DEFAULT 2000;
DECLARE eof boolean default FALSE;
DECLARE _AvTotal INT;
DECLARE _prv_AvTotal INT default 0;
DECLARE _YOY_PCT INT;
DECLARE _YOY_USD INT;
DECLARE _in_pct INT;
DECLARE _in_usd INT;
DECLARE _last INT;
DECLARE _id INT;
DECLARE _idx INT;
DECLARE _prv_id INT;
DECLARE get_FIPSAPNCombo CURSOR
FOR
SELECT 0, '0', 2000, 1
UNION
SELECT id, FIPSAPNCombo, rollyear, AvTotal
FROM AvHistory_06037 AS h
WHERE substr(h.FIPSAPNCombo,1,8) = _in_FIPSAPNCombo_8
AND h.AvTotal > 0
ORDER BY 2, 3
;
DECLARE CONTINUE HANDLER
FOR NOT FOUND
SET eof = TRUE;
SELECT YOY_PCT, YOY_USD INTO _in_pct, _in_usd FROM AlertSearches WHERE id = _in_AlertSearchesID;
START TRANSACTION;
OPEN get_FIPSAPNCombo;
doit: LOOP
fetch get_FIPSAPNCombo INTO _id, _FIPSAPNCombo, _Rollyear, _AvTotal;
SET _YOY_USD = _AvTotal - _prv_AvTotal;
SET _YOY_PCT = ROUND((((_AvTotal - _prv_AvTotal) / _prv_AvTotal) * 100),0);
IF eof = TRUE THEN
LEAVE doit;
END IF;
IF _FIPSAPNCombo != _prv_FIPSAPNCombo THEN SET _first_Rollyear = _Rollyear; ELSE SET _first_Rollyear = '2000'; END IF;
IF _YOY_PCT >= _in_pct AND _YOY_USD >= _in_usd and _Rollyear != _first_Rollyear THEN
BEGIN
DELETE FROM AvHistoryAlerts WHERE FIPSAPNCombo = _FIPSAPNCombo AND AlertSearchesID = _in_AlertSearchesID;
INSERT INTO AvHistoryAlerts VALUES (_id, _FIPSAPNCombo, '06037', _Rollyear, _AvTotal, _YOY_PCT, _YOY_USD, _prv_id, _prv_AvTotal, _in_AlertSearchesID);
UPDATE AvHistory SET CausalTransfer = NULL WHERE FIPSAPNCombo = _FIPSAPNCombo;
UPDATE AvHistory SET CausalTransfer = 'Green' WHERE id = _id;
SET i = i + 1;
IF mod(i,1000) = 0 THEN insert into error_log VALUES (concat(_in_FIPSAPNCombo_8, ' > calc_YOY_increase i = ', i, ' ',now())); COMMIT; START TRANSACTION; END IF;
END;
END IF;
SET _prv_FIPSAPNCombo = _FIPSAPNCombo;
SET _prv_AvTotal = _AvTotal;
SET _prv_id = _id;
SET eof = FALSE;
END LOOP;
COMMIT;
CLOSE get_FIPSAPNCombo;
END
I thought that the procedure would commit every 1000 records processed and that this would be reflected in the error_log table in real time.
But, the data is only available after the procedure finishes completely and then all the records show up all at once in the error_log table.
That, by itself, is not the issue: what I am concerned about is the the procedure seems to dwell on "waiting for handler commit" for a long time at the end of the procedure.
So, I'm not sure what's going on: is it storing everything in memory or in a temp table or file and then needs a lot if time for cleanup?
I thought that a commit would clear all that... but it does not seem to commit until the end of the whole procedure
Any ideas?

Related

Cursor with JOIN and WHERE MySQL

I am trying to create a repetition in 'cursor' that uses 'join', but it doesn't work.
Already tested without 'join' and works normally.
My code:
DECLARE hvDataAtual DATE;
DECLARE manDataAtual DATETIME;
DECLARE motCodAtual INT;
DECLARE done INT DEFAULT FALSE;
DECLARE Crs_vei_man
CURSOR FOR (
SELECT hvedatageracao, hvedatageracao, hvemotorista
FROM tableone INNER JOIN tabletwo ON hveveiculo = manveiculo
WHERE SUBSTRING(mandata, 1, 10) = SUBSTRING(hvedatageracao, 1, 10) LIMIT 10);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN Crs_vei_man;
myloop: LOOP
FETCH Crs_vei_man
INTO hvDataAtual,
manDataAtual,
motCodAtual;
-- Insert 1 row (null, null, null) ?
INSERT INTO testes VALUES(hvDataAtual, manDataAtual, motCodAtual);
IF done THEN
LEAVE myloop;
END IF;
IF motCodAtual > 0 THEN
INSERT INTO testes VALUES('hvDataAtual', 'manDataAtual', 'motCodAtual');
END IF;
END LOOP;
CLOSE Crs_vei_man;
This code is in a stored procedure.
What is happening?

MySQL Inner cursor executes only one time

DELIMITER $$
CREATE PROCEDURE `remove_schedule_duplicate` ()
BEGIN
BLOCK1 : BEGIN
DECLARE finished INTEGER DEFAULT 0;
DECLARE schedule_id CHAR(36);
DECLARE gamePk INTEGER;
DECLARE keep_entry TINYINT(1);
DECLARE scheduleDuplicate CURSOR FOR SELECT game_pk FROM schedule where is_active = 1 group by game_pk,home_team_id,away_team_id,venue_id having count(game_pk) > 1 limit 2;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = 1;
OPEN scheduleDuplicate;
get_schedule_duplicate: LOOP
FETCH scheduleDuplicate INTO gamePk;
IF finished = 1 THEN
LEAVE get_schedule_duplicate;
END IF;
**BLOCK2 : BEGIN
DECLARE block_finished INTEGER DEFAULT 0;
DECLARE blockDuplicate CURSOR FOR SELECT id FROM schedule where game_pk = gamePk and is_active = 1;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET block_finished = 1;
OPEN blockDuplicate;
block_schedule_duplicate: LOOP
FETCH blockDuplicate INTO schedule_id;
IF block_finished = 1 THEN
LEAVE block_schedule_duplicate;
END IF;
IF keep_entry = 0 THEN
UPDATE schedule set is_active = 0 where id = schedule_id;
END IF;
END LOOP block_schedule_duplicate;
CLOSE blockDuplicate;
END BLOCK2;**
END LOOP get_schedule_duplicate;
CLOSE scheduleDuplicate;
END BLOCK1;
END
$$
The problem is Innerloop first time execute well after that block_finished always be 1 so. always it quits the inner block.
How to resolve this issue. What i do ? Some one help me to resolve the issue.
I have Changed My Stored procedure like this:
DELIMITER $$
CREATE PROCEDURE `remove_schedule_duplicate`()
BEGIN
DECLARE finished INTEGER DEFAULT 0;
DECLARE schedule_id,temp CHAR(36) DEFAULT NULL;
DECLARE gamePk INTEGER;
DECLARE keep_entry TINYINT(1);
DECLARE scheduleDuplicate CURSOR FOR SELECT game_pk,id FROM schedule where game_pk in (SELECT game_pk FROM schedule where is_active = 1 group by game_pk,home_team_id,away_team_id,venue_id having count(game_pk) > 1) and is_active = 1 order by game_pk;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = 1;
OPEN scheduleDuplicate;
get_schedule_duplicate: LOOP
FETCH scheduleDuplicate INTO gamePk,schedule_id;
IF finished = 1 THEN
LEAVE get_schedule_duplicate;
END IF;
IF ((temp IS NULL) AND (gamePk IS NOT NULL)) OR ((temp IS NOT NULL) AND (temp <> gamePk)) THEN
SET temp = gamePk;
SET keep_entry = 1;
#ELSE IF temp IS NOT NULL AND temp = gamePk THEN
ELSE
SET keep_entry = 0;
END IF;
IF keep_entry = 0 THEN
UPDATE schedule set is_active = 0 where id = schedule_id;
END IF;
END LOOP get_schedule_duplicate;
CLOSE scheduleDuplicate;
END$$
DELIMITER ;

mysql procedure : syntax, syntax, syntax ...... too hard to find

Here is my procedure :
delimiter //
drop procedure if exists migContactToActor;
create procedure migContactToActor()
begin
declare vctaid int;
declare firstname char(255);
declare lastname char(255);
declare phone char(255);
declare cellphone char(255);
declare fax char(255);
declare mail char(255);
declare location char(255);
declare extcode char(255);
declare vcpyid int;
declare vquaid int;
declare userModif char(255);
declare entityIdResp int;
declare niveauCreat char(255) default "contactMigration2.21";
declare userCreat char(255) default "contactMigration2.21";
declare backIdResp int;
declare userIdResp int;
declare zipcode char(255);
declare country char(255);
declare dateModif char(255);
declare dateCreated char(255);
declare dateRelation char(255);
declare groupLabel char(255);
declare adminLogId int;
declare finContact boolean default 0;
declare ctt int default 1; /* decompte contacts */
declare entityName char(255) default ''; /* entite courante du user a linker */
declare entityId int default 0; /* et son Id */
declare vctaidPrev int default 0;
declare curs1 cursor for select
ctaid, ctafirstname,ctalastname,ctaphone,ctacellphone,ctaemail,
substr(concat_ws(' ',ctaaddress,ctaaddress2,ctacity,ctazipcode),1,255),ctacode, cpyid,quaid,C.enoid_resp,
ctafax,C.actid_bck,ctadate_created,ctacountry,ctadate_modified,C.actid_resp,ctauser_modified, G.hemlabel, R.sync_update
from contact C
left join hd_contact_group_relationship R on R.hbhid = C.ctaid
left join hd_contact_group G on G.hemid = R.hemid
where !ifnull(C.ctaflagdeleted,0) and !ifnull(G.hemflag_deleted,0);
declare continue handler for not found set finContact = 1;
declare continue handler for sqlexception
begin
rollback;
end;
select '======== debut ...';
drop table if exists ent221;
create temporary table ent221 as select enoid, enoname from entityowner where enoname like 'CLI.CG_%.CLI';
alter table ent221 add index(enoname); /* not unique in dev, fetch the first in prod if any */
set autocommit = 0; /* TTT */
start transaction; /* TTT */
open curs1;
contactloop:loop
fetch curs1 into vctaid, firstname,lastname,phone,cellphone,mail,location,extcode,vcpyid,vquaid,entityIdResp,
fax,backIdResp,dateCreated,country,dateModif,userIdResp,userModif,groupLabel,dateRelation;
if finContact then
close curs1;
leave contactloop;
end if;
if vctaid = vctaidPrev then
set vctaidPrev = vctaid;
if (mod(ctt,100) = 0) then
set #msg = concat(ctt, ' contacts migrated');
select #msg;
end if;
update OPMSequence set counter = (#wbuf1 := counter) + 1 where name = 'ACTOR';
insert into actor (actid,cpyid,actlastname,actfirstname,actemail,actphone,actmobilephone,
actlocalisation, actid_responsible_owner,actid_backup_owner,enoid_responsible_owner,
actuser_created,actniveau_created, actdate_created,actuser_modified,
actdate_modified,actcountry,actfax,actqualifid,actexternal_code)
values
(#wbuf1,vcpyid,lastname,firstname,mail,phone,cellphone,location,userIdResp,backIdResp,entityIdResp,
userCreat,niveauCreat,dateCreated,userModif,dateModif,country,fax,vquaid,extcode);
update adminLog set adltype = 'User', adlobject_id = #wbuf1,
adlobject_description = concat(adlobject_description,'//oldContact=',vctaid)
where adlobject_id = vctaid and adltype = 'Contact';
end if;
if groupLabel is not null then
set #entName = concat('CLI.CG_',groupLabel,'.CLI);
select enoid from ent221 where enoname = #entName limit 1 into #wbuf2;
if #wbuf2 is not null then
insert into actorentityrelationship (enoid,actid,aerdate_created,aerniveau_created) values
(#buf2,#wbuf1,dateRelation,niveauCreat);
enf if;
end if;
set ctt = ctt + 1;
end loop contactloop;
rollback; /* TTT */
end //
delimiter ;
call migContactToActor;
drop procedure migContactToActor;
Why this ???
Query OK, 0 rows affected, 1 warning (0.00 sec)
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''.CLI);
select enoid from ent221 where enoname = #entName limit 1 into #wbuf2' at line 83
I think you missed the quotes
set #entName = concat('CLI.CG_',groupLabel,'.CLI);
with quotes
set #entName = concat('CLI.CG_',groupLabel,'.CLI');

splitting text data in mysql

i want to split my row something like this:
example:
id data
1 23#4#
2 2#4#5#
to be like this
id data
1 23
1 4
2 2
2 4
2 5
i have looking for this problem in stack overflow but the solution is not satisfaction me. please help me.
i try to make a store procedure in mysql, the code it's ok no error, but the result is not ok.
drop procedure if exists pecah_lokasi_sp;
create procedure pecah_lokasi_sp()
begin
DECLARE ii INT;
DECLARE ki varchar(255);
DECLARE ulang int DEFAULT 1;
DECLARE hasil varchar(255) DEFAULT '';
DECLARE selesai INT DEFAULT FALSE;
DECLARE kursor CURSOR FOR
SELECT id_iklan, lokasi_kerja_utama
FROM iklan_lowongan_tb
WHERE status = 'Aktif';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET selesai = TRUE;
DELETE FROM temp_lok_tb;
OPEN kursor;
read_loop: LOOP
FETCH kursor INTO ii, ki;
IF selesai THEN
LEAVE read_loop;
END IF;
WHILE (ulang < LENGTH(ki)) DO
IF SUBSTRING(ki, ulang, 1) <> '#' THEN
SET hasil = CONCAT(hasil, SUBSTRING(ki, ulang, 1));
ELSE
INSERT INTO temp_lok_tb (id_iklan, id_kota) VALUES(ii, hasil);
SET hasil = '';
END IF;
SET ulang = ulang+1;
END WHILE;
SET ulang = 1;
END LOOP read_loop;
CLOSE kursor;
select * from temp_lok_tb;
end;
i think i can solve my problem myself.
this is my revision code, the result is true.
drop procedure if exists pecah_lokasi_sp;
create procedure pecah_lokasi_sp()
begin
DECLARE ii INT;
DECLARE ki varchar(255);
DECLARE ulang int DEFAULT 1;
DECLARE hasil varchar(255) DEFAULT '';
DECLARE selesai INT DEFAULT FALSE;
DECLARE jmlkarakter int;
DECLARE kursor CURSOR FOR
SELECT id_iklan, lokasi_kerja_utama
FROM iklan_lowongan_tb
WHERE status = 'Aktif';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET selesai = TRUE;
DELETE FROM temp_lok_tb;
OPEN kursor;
read_loop: LOOP
FETCH kursor INTO ii, ki;
IF selesai THEN
LEAVE read_loop;
END IF;
SET jmlkarakter = LENGTH(ki) - LENGTH(REPLACE(ki, '#', '')) ;
WHILE (ulang <= jmlkarakter) DO
IF ulang = 1 THEN
INSERT INTO temp_lok_tb(id_iklan, id_kota) VALUES(ii, SUBSTRING_INDEX(ki,'#',1));
ELSE
INSERT INTO temp_lok_tb(id_iklan, id_kota) VALUES(ii, SUBSTRING_INDEX(SUBSTRING_INDEX(ki,'#', ulang),'#',-1));
END IF;
SET ulang = ulang+1;
END WHILE;
SET ulang = 1;
END LOOP read_loop;
CLOSE kursor;
select * from temp_lok_tb;
end;

populating table in stored procedure, mysql

Can anyone shed light on why this will not loop through the function and populate the temp table? Ive tried a number of things and at best can only get the first value to populate. I'm trying to take a value and a ":" separated string (queried in this procedure) to populate a table so I can reference it in a larger query. The function SPLIT_STR works great on its own but I cant seem to increment value "a" so that it separates ALL values per field per value.
BEGIN
DECLARE platform_val VARCHAR(255);
DECLARE productName_val VARCHAR(255);
DECLARE no_more_rows BOOLEAN;
DECLARE num_rows INT DEFAULT 0;
DECLARE loop_cntr INT DEFAULT 0;
DECLARE str VARCHAR(255);
DECLARE a INT DEFAULT 1;
DECLARE cur1 CURSOR FOR SELECT
ProductName,
ProductPlatforms
FROM Feed
limit 10;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_more_rows = TRUE;
OPEN cur1;
select FOUND_ROWS() into num_rows;
the_loop: LOOP
FETCH cur1
INTO productName_val,
platform_val;
SET str = platform_val;
WHILE a< num_rows DO
SET str=SPLIT_STR(str,":",a);
insert into temp values (productName_val, str);
SET a=a+1;
END WHILE;
END LOOP the_loop;
END
This is the idea I came up with
CREATE PROCEDURE `col2lines`()
BEGIN
declare v_platform,v_productName,v_platformAll varchar(255) default null;
declare v_finished int default 0;
declare curs cursor for select ProductName,ProductPlatforms from Feed limit 10;
declare continue handler for not found set v_finished = 1;
drop temporary table if exists temp_table;
create temporary table temp_table (
productName varchar(255),
platform varchar(255)
);
open curs;
parent: LOOP
fetch curs into v_productName, v_platformAll;
if (v_finished = 1) then LEAVE parent; end if;
child: LOOP
set v_platform = substring_index(v_platformAll, '::', 1);
set #strlen = char_length(v_platform);
if (#strlen > 0) then
insert into temp_table values (v_productName, v_platform);
set v_platformAll = substr(v_platformAll, #strlen + 3);
iterate child;
end if;
LEAVE child;
END LOOP child;
iterate parent;
END LOOP parent;
close curs;
select * from temp_table;
END