Skip to content

Instantly share code, notes, and snippets.

@petronioamaral
Created August 5, 2020 14:57
Show Gist options
  • Save petronioamaral/4103cd698c72bf20dcf76090356a9947 to your computer and use it in GitHub Desktop.
Save petronioamaral/4103cd698c72bf20dcf76090356a9947 to your computer and use it in GitHub Desktop.
procedure mysql update with cursor
DROP PROCEDURE IF EXISTS dorepeat

CREATE PROCEDURE dorepeat()
BEGIN
DECLARE someId INT;
DECLARE neighborhood LONGTEXT;
DECLARE done INT DEFAULT FALSE;
DECLARE cur1 CURSOR FOR select id_bairro,bairro from cepbr_bairro ;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;     

OPEN cur1;

read_loop: LOOP

FETCH cur1 INTO someId, neighborhood;

update cepbr_bairro set nome_sem_acento = fn_remove_accents(neighborhood) where id_bairro = someId;

IF done THEN

CLOSE cur1;
  LEAVE read_loop;
END IF;

END LOOP read_loop;
END;

CALL dorepeat();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment