OPEN c1;
city_loop: LOOP
FETCH c1 into s_CC,s_REG;
set t1= concat('\'',concat(s_CC,'\''));
SET s_countrycode = concat_WS(',',s_countrycode,t1);
IF l_done=1 THEN
LEAVE city_loop;
END IF;
END LOOP city_loop;
SET @LOC = s_countrycode;
SET @SQL ='SELECT * from city where countrycode IN (';
SET @SQL = concat(concat(@SQL,@LOC),') AND city.population > 10000 AND city.population < 500000 ');
PREPARE s1 FROM @SQL;
EXECUTE s1;
CLOSE c1;