Quiero dar salida a los datos de cada tabla que contiene una columna first_name
. Junté el siguiente procedimiento, pero en mi ciclo, mysql interpreta literalmente el nombre de la tabla en lugar de evaluar la variable table_name
. ¿Cuál es la solución?MySQL loop through tablas
delimiter //
drop procedure if exists hunt //
create procedure hunt()
begin
DECLARE done int default false;
DECLARE table_name CHAR(255);
DECLARE cur1 cursor for SELECT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = "wholesale_production" and COLUMN_NAME LIKE "%first%" ;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
open cur1;
myloop: loop
fetch cur1 into table_name;
if done then
leave myloop;
end if;
select * from `wholesale_production`.table_name where created_at >= '2012-10-01';
end loop;
close cur1;
end //
delimiter ;
call hunt();
Tengo un problema similar. Si no me importa filtrar el nombre de la tabla para que sea 'LIKE '% first%'', ¿puedo usar 'SHOW TABLES' en lugar de' SELECT TABLE_NAME ...'? – Kal
table_name es un nombre reservado. debe cambiarse a un nombre diferente. –