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).
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.