2012-10-04 12 views
7

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(); 

Respuesta

9

Prueba esto:

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; 
     set @sql = CONCAT('select * from `wholesale_production`.', table_name, ' where created_at >= '2012-10-01'); 
     prepare stmt from @sql; 
     execute stmt; 
     drop prepare stmt; 
    end loop; 

    close cur1; 
end // 

delimiter ; 

call hunt(); 
+1

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

+1

table_name es un nombre reservado. debe cambiarse a un nombre diferente. –

-1

en cur1 está utilizando TABLE_NAME no trate de usar un nombre real de la tabla

+1

Pero no se conoce la tabla, está obteniendo nombres de tabla de INFORMATION_SCHEMA. – Barmar

0

un poco de edición de los anteriores a itertate ahtoug todas las tablas y seleccione ellos.

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.TABLES 
        WHERE table_schema ='mbu4u'; 
    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; 
     set @sql = CONCAT('select * from `mbu4u`.',table_name); 
     prepare stmt from @sql; 
     execute stmt; 
     drop prepare stmt; 
    end loop; 

    close cur1; 
end // 

delimiter // 
0

nombre_tabla es un contador reservados Use otro nombre de variable en "DECLARE CHAR nombre_tabla (255);"