Skip to content

Instantly share code, notes, and snippets.

@gbarreiro
Created August 20, 2020 15:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gbarreiro/84a2d437c6c4a75256a15399e821a64a to your computer and use it in GitHub Desktop.
Save gbarreiro/84a2d437c6c4a75256a15399e821a64a to your computer and use it in GitHub Desktop.
MySQL cheatsheet: cursors
-- 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