Created
August 20, 2020 15:04
-
-
Save gbarreiro/84a2d437c6c4a75256a15399e821a64a to your computer and use it in GitHub Desktop.
MySQL cheatsheet: cursors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Declaring the cursor | |
DECLARE end_cursor BOOLEAN DEFAULT FALSE; -- will turn true when the cursor reaches the last tuple | |
DECLARE my_cursor CURSOR FOR SELECT * from Students; -- a cursor iterates through the result of a SELECT or CALL statement | |
DECLARE CONTINUE HANDLER FOR NOT FOUND SET end_cursor = TRUE; | |
-- Iterating through the cursor | |
WHILE NOT end_cursor DO | |
FETCH cursor INTO @my_student; | |
-- Do something with the current row -- | |
END WHILE; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment