SQL Tutorial - Cursors

Publicado el: 02 junio 2022
en el canal de: Develpreneur
21
0

1. Cursors

DELIMITER //
CREATE PROCEDURE cursorExample (
)
BEGIN
DECLARE done INTEGER DEFAULT 0;
DECLARE theName varchar(100) DEFAULT "";

-- declare cursor
DEClARE curName
CURSOR FOR
SELECT username FROM app_user;

-- declare NOT FOUND handler
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET done = 1;

OPEN curName;
myLoop: LOOP
FETCH curName INTO theName;
IF finished = 1 THEN
LEAVE myLoop;
END IF;

-- provide output
select theName;
END LOOP myLoop;

-- Clean up when we are done
CLOSE curName;

END//
DELIMITER ;

DELIMITER //
CREATE OR REPLACE PROCEDURE cursorExample (
)
BEGIN
DECLARE done INTEGER DEFAULT 0;
DECLARE theName varchar(100) DEFAULT "";
DECLARE output varchar(1000) DEFAULT "";

-- declare cursor
DECLARE myCursor
CURSOR FOR
SELECT username FROM app_user;

-- declare NOT FOUND handler
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET done = 1;

OPEN myCursor;
myLoop: LOOP
FETCH myCursor INTO theName;
IF done = 1 THEN
LEAVE myLoop;
END IF;

-- provide output
set theName = concat(theName,", ");
set output = concat(output,theName);
END LOOP myLoop;

-- Clean up when we are done
CLOSE myCursor;
select output;

END//
DELIMITER ;

DELIMITER //
CREATE OR REPLACE PROCEDURE cursorExample (
)
BEGIN
DECLARE done INTEGER DEFAULT 0;
DECLARE theName varchar(100) DEFAULT "";
DECLARE theLogin varchar(100) DEFAULT "";
DECLARE output varchar(1000) DEFAULT "";

-- declare cursor
DECLARE myCursor
CURSOR FOR
SELECT userName,userLogin FROM app_user;

-- declare NOT FOUND handler
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET done = 1;

OPEN myCursor;
myLoop: LOOP
FETCH myCursor INTO theName,theLogin;
IF done = 1 THEN
LEAVE myLoop;
END IF;

-- provide output
set theLogin = concat(" [",theName,"]");
set theName = concat(theName,theLogin," | ");
set output = concat(output,theName);
END LOOP myLoop;

-- Clean up when we are done
CLOSE myCursor;
select output;

END//
DELIMITER ;


En esta página del sitio puede ver el video en línea SQL Tutorial - Cursors de Duración hora minuto segunda en buena calidad , que subió el usuario Develpreneur 02 junio 2022, comparta el enlace con amigos y conocidos, en youtube este video ya ha sido visto 21 veces y le gustó 0 a los espectadores. Disfruta viendo!