mysql loop through databases and execute query

Solutions on MaxInterview for mysql loop through databases and execute query by the best coders in the world

showing results for - "mysql loop through databases and execute query"
Ciel
08 Sep 2017
1delimiter //
2DROP PROCEDURE IF EXISTS create_procedures//
3CREATE PROCEDURE create_procedures()
4BEGIN
5    DECLARE done INT DEFAULT 0;
6    DECLARE db VARCHAR(255);
7    DECLARE appDBs CURSOR FOR SELECT schema_name FROM information_schema.schemata WHERE schema_name LIKE 'application_%';
8    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
9
10    SET @procName = "simpleproc"; -- Change this to your proc name
11
12    SET @output = "delimiter //";
13
14    OPEN appDBs;
15    REPEAT
16        FETCH appDBs INTO db;
17        IF NOT done THEN
18            -- Replace this procedure declaration with your procedure.
19            -- Make sure to keep the ',db,' syntax there.
20            -- You should really only have to change the parameters
21            -- and the stuff between the BEGIN and END clauses.
22            SET @output = CONCAT(@output,'
23    DROP PROCEDURE IF EXISTS ',db,'.',@procName,'//
24    CREATE PROCEDURE ',db,'.',@procName,'()
25        BEGIN
26            SELECT 1;
27        END//');
28
29        END IF;
30    UNTIL done END REPEAT;
31
32    CLOSE appDBs;
33
34    SET @output = CONCAT(@output,'\ndelimiter ;');
35
36    SELECT @output AS procs;
37END//
38delimiter ;
39