USE test;
DELIMITER $$
DROP PROCEDURE IF EXISTS example4$$
CREATE PROCEDURE example4( )
BEGIN

DECLARE dname,natl,current_natl VARCHAR(255);
DECLARE hasnext INT DEFAULT TRUE;

DECLARE c CURSOR FOR
SELECT displayname,nationality
FROM moma.artists
WHERE displayname like 'm% m%'
AND nationality != ''
ORDER BY nationality,displayname;

DECLARE CONTINUE HANDLER FOR NOT FOUND SET hasnext = FALSE;

OPEN c;
FETCH c INTO dname, natl;
SET current_natl = '';
WHILE hasnext DO
  IF natl != current_natl THEN
    SET current_natl = natl;
    SELECT current_natl;
  END IF;
  SELECT concat('    ',dname);
  FETCH c INTO dname, natl;
END WHILE;

END $$
DELIMITER ;
call example4();