SQL Tutorial - Cursors

Pubblicato il: 02 giugno 2022
sul canale di: 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 ;


In questa pagina del sito puoi guardare il video online SQL Tutorial - Cursors della durata di ore minuti seconda in buona qualità , che l'utente ha caricato Develpreneur 02 giugno 2022, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 21 volte e gli è piaciuto 0 spettatori. Buona visione!