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

DECLARE dname VARCHAR(255);
DECLARE hasnext INT DEFAULT TRUE;

DECLARE c CURSOR FOR
SELECT displayname
FROM moma.artists
WHERE nationality = 'American'
LIMIT 20;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET hasnext = FALSE;

OPEN c;
rloop: LOOP
  FETCH c INTO dname;
  IF NOT hasnext THEN
    LEAVE rloop;
  END IF;
  SELECT dname;
END LOOP;

END $$
DELIMITER ;
call example3();