CREATE DEFINER=`root`@`localhost` PROCEDURE `SPAllDBEmpresasCreadas`()
BEGIN
	declare done int default 0;
	DECLARE exit_loop       BOOLEAN;
	declare usuario_id,empresa_id int;
	declare document,loginuser,nombre VARCHAR(250);
	declare union_query TEXT DEFAULT '';
	DECLARE db_name VARCHAR(250);
    
    
    DECLARE tdbabuela CURSOR FOR 
	SELECT cuit,field_name1 FROM dbtaxes.DBabuelas;

   

	DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;
	OPEN tdbabuela;
	get_database: REPEAT

	FETCH tdbabuela INTO document,nombre;


			SET union_query = concat(union_query,'SELECT count(*),"',nombre,'"as Contador FROM ', '`db',document,'`', '.person_configurations',' UNION ALL ');


	UNTIL done END REPEAT;

  	CLOSE tdbabuela;

  	SET union_query = TRIM(TRAILING ' UNION ALL ' FROM union_query);

  	-- select union_query;
    
    set @queryfinal = union_query;

  	PREPARE stmt1 FROM @queryfinal;
    EXECUTE stmt1;
    DEALLOCATE PREPARE stmt1;

  END