Good afternoon.
An example of the creation of a PROCEDURE
with CURSOR
, basically a cake recipe. Do not forget to always pay attention to the change of delimiter before the creation of any TRIGGER
, PROCEDURE
or FUNCTION
.
DROP PROCEDURE IF EXISTS pr_exemplo_cursor;
DELIMITER $
CREATE PROCEDURE pr_exemplo_cursor()
BEGIN
DECLARE Id INT;
DECLARE Name VARCHAR(80);
DECLARE done INT DEFAULT FALSE;
DECLARE cur1 CURSOR FOR SELECT Id, Name FROM users;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur1;
read_loop: LOOP
IF done THEN
LEAVE read_loop;
ELSE
SELECT 'DENTRO DO LAÇO DE REPETIÇÃO';
END IF;
FETCH cur1 INTO Id, Name;
END LOOP;
close cur1;
END$
I recommend you take a look at this course of Mysql, it covers very well most topics.
That’s what I thought, but when adding the code between Begin and the end the error persists
– Vinícius Souza
Buddy, use
DELIMITER $
to change the delimiter before the creation of thePROCEDURE
– Bruno Henrique