Skip to content

Instantly share code, notes, and snippets.

@arieljannai
Created September 7, 2016 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arieljannai/8325a13f1e2d8f80ed5cb6b816754a3d to your computer and use it in GitHub Desktop.
Save arieljannai/8325a13f1e2d8f80ed5cb6b816754a3d to your computer and use it in GitHub Desktop.
Loop all tables for something
DROP PROCEDURE IF EXISTS loopTables;
DELIMITER //
CREATE PROCEDURE `loopTables` ()
BEGIN
DECLARE curr_table VARCHAR(200);
DECLARE cursor_tables CURSOR FOR select table_name from information_schema.tables where table_schema = (SELECT DATABASE());
OPEN cursor_tables;
grant_loop: LOOP
FETCH cursor_tables INTO curr_table;
-- do something
IF done THEN
LEAVE grant_loop;
END IF;
END LOOP;
CLOSE cursor_tables;
END //
DELIMITER ;
CALL loopTables();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment