Reputation: 8484
I've got a set of tables named results_%, all with the same structure.
I would like to add a index to this tables.
I can get the alter statement for each table as a row of a select query result but I don't know how to execute this statements:
select concat( 'alter table ', test_db.table_name, ' add index `did` (`did`);' ) as statement
from information_schema.tables test_db
where test_db.table_name like 'results_%';
What am I missing?
The ouput (which I would like to execute instead of just have it displayed to me):
+---------------------------------------------------------+
| statement |
+---------------------------------------------------------+
| alter table results_Em7777_spa add index `did` (`did`); |
| alter table results_KaEng_eng add index `did` (`did`); |
| alter table results_Ka_spa add index `did` (`did`); |
| alter table results_Mc_spa add index `did` (`did`); |
| alter table results_Mo_eng add index `did` (`did`); |
| alter table results_Pe_eng add index `did` (`did`); |
| alter table results_SU_spa add index `did` (`did`); |
| alter table results_Ta_spa add index `did` (`did`); |
| alter table results_ba_eng add index `did` (`did`); |
| alter table results_br_eng add index `did` (`did`); |
| alter table results_ca_spa add index `did` (`did`); |
| alter table results_ch_spa add index `did` (`did`); |
| alter table results_da_spa add index `did` (`did`); |
| alter table results_ga_eng add index `did` (`did`); |
| alter table results_ge_spa add index `did` (`did`); |
| alter table results_gk_eng add index `did` (`did`); |
+---------------------------------------------------------+
16 rows in set (0.00 sec)
[EDIT]
I tried:
drop procedure if exists altlike;
delimiter //
create procedure altlike()
begin
set group_concat_max_len = 65535;
select @altrlk:= concat( 'alter table ', test_db.table_name , ' add index `did` (`did`);' )
from information_schema.tables test_db
where test_db.table_name like "results_%";
prepare statement from @altrlk;
execute statement;
end //
delimiter ;
call altlike();
But still no luck: It only alters the last matched table (results_gk_eng).
Upvotes: 4
Views: 6703
Reputation: 8484
drop procedure if exists `altlike`;
DELIMITER //
CREATE PROCEDURE `altlike` ()
BEGIN
DECLARE a,c VARCHAR(256);
DECLARE b INT;
DECLARE cur1 CURSOR FOR select concat(test_db.table_name)
from information_schema.tables test_db
where test_db.table_name like 'results_%';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET b = 1;
DECLARE CONTINUE HANDLER FOR 1061 SET b = 0;
OPEN cur1;
SET b = 0;
WHILE b = 0 DO
FETCH cur1 INTO a;
IF b = 0 THEN
SET @c = concat ('ALTER IGNORE TABLE `', a, '` ADD INDEX `did` (`did`)');
PREPARE stmt1 FROM @c;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
END IF;
END WHILE;
CLOSE cur1;
END //
call altlike();
Upvotes: 4
Reputation: 4637
You basically printing out lines of string out of the DB, it would not automatically execute it just because it looks like a sql statement;
What you can do is either use a programming language to execute line by line as you get the results back.
Or throw this into a stored procedure where it feed a secondary block of execution.
Example: FROM http://net.tutsplus.com/tutorials/an-introduction-to-stored-procedures/ read more about it.
DELIMITER //
CREATE PROCEDURE `proc_CURSOR` (OUT param1 INT)
BEGIN
DECLARE a, b, c INT;
DECLARE cur1 CURSOR FOR SELECT col1 FROM table1;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET b = 1;
OPEN cur1;
SET b = 0;
SET c = 0;
WHILE b = 0 DO
FETCH cur1 INTO a;
IF b = 0 THEN
SET c = c + a;
END IF;
END WHILE;
CLOSE cur1;
SET param1 = c;
END //
Upvotes: 2