Search
 
SCRIPT & CODE EXAMPLE
 

SQL

mysql loop through databases and execute query

delimiter //
DROP PROCEDURE IF EXISTS create_procedures//
CREATE PROCEDURE create_procedures()
BEGIN
    DECLARE done INT DEFAULT 0;
    DECLARE db VARCHAR(255);
    DECLARE appDBs CURSOR FOR SELECT schema_name FROM information_schema.schemata WHERE schema_name LIKE 'application_%';
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

    SET @procName = "simpleproc"; -- Change this to your proc name

    SET @output = "delimiter //";

    OPEN appDBs;
    REPEAT
        FETCH appDBs INTO db;
        IF NOT done THEN
            -- Replace this procedure declaration with your procedure.
            -- Make sure to keep the ',db,' syntax there.
            -- You should really only have to change the parameters
            -- and the stuff between the BEGIN and END clauses.
            SET @output = CONCAT(@output,'
    DROP PROCEDURE IF EXISTS ',db,'.',@procName,'//
    CREATE PROCEDURE ',db,'.',@procName,'()
        BEGIN
            SELECT 1;
        END//');

        END IF;
    UNTIL done END REPEAT;

    CLOSE appDBs;

    SET @output = CONCAT(@output,'
delimiter ;');

    SELECT @output AS procs;
END//
delimiter ;
Comment

PREVIOUS NEXT
Code Example
Sql :: sql creating tables 
Sql :: sql into 
Sql :: how to put 0 or 000 depending IDCustomer length in sql server 
Sql :: default column value in sql same as another column laravel 
Sql :: sql searching via key word 
Sql :: insert command in sql 
Sql :: T-SQL and the WHERE LIKE %Parameter% clause 
Sql :: copy table db 
Sql :: php select data from mysql database without column name 
Sql :: can you put a break command in sql 
Sql :: mamp mysql password 
Sql :: sql server get number of working days in a month 
Sql :: sql lowest number possible 
Sql :: Oracle SQL join three tables and group by column 
Sql :: postgresql install with ansible 
Sql :: incorrect datetime value sql table error 1292 
Sql :: oracle select 
Sql :: what is key in sql 
Sql :: sorting desc in sql 
Sql :: mysql replace empty string with null 
Sql :: sqlite3.OperationalError: near "7": syntax error 
Sql :: with transaction.atomic(): 
Sql :: how to link java and mysql 
Sql :: sql year 
Sql :: sql select column names starting with 
Sql :: how to use 3 fields as primary key in sql tables? 
Sql :: like sql for second letter of the family name 
Sql :: db connection using sql client in dot net 
Sql :: postgres multiple left join causing duplicates jsonb_agg 
Sql :: sql server udf performance 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =