Skip to content

Commit 2879777

Browse files
authored
Create LoopsAndCursors.SQL
This stored procedure updates a field from a table from coincident data from another.
1 parent ba50acf commit 2879777

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

LoopsAndCursors.SQL

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
## This stored procedure updates a field between tables comparing a value from a second
2+
3+
CREATE DEFINER=`root`@`localhost` PROCEDURE `actualiza_entregas`()
4+
BEGIN
5+
DECLARE nom_entregado varchar(255);
6+
DECLARE fin integer default 0;
7+
8+
#select data for iteration
9+
DECLARE entregados_cursor CURSOR FOR
10+
SELECT municipio FROM entregados;
11+
12+
#exit condition for entregados_cursor
13+
DECLARE CONTINUE HANDLER FOR NOT FOUND SET fin=1;
14+
15+
#open cursor
16+
OPEN entregados_cursor;
17+
18+
#begin loop
19+
get_entregados: LOOP
20+
#set the value of entregados_cursor to variable nom_entregado
21+
FETCH entregados_cursor INTO nom_entregado;
22+
IF fin = 1 THEN
23+
#if condition ... exit loop
24+
LEAVE get_entregados;
25+
END IF;
26+
27+
#update the field when nom_entregado like nom loc in second table
28+
UPDATE georeferencia set registro = 1 where nom_loc like nom_entregado;
29+
30+
#end loop
31+
END LOOP get_entregados;
32+
33+
#close the cursor
34+
close entregados_cursor;
35+
36+
END

0 commit comments

Comments
 (0)