DELIMITER \\
DROP PROCEDURE IF EXISTS dataset_get_new_key\\
CREATE PROCEDURE dataset_get_new_key(out o_dataset_access_key char(32))
BEGIN

    declare l_good_key_found boolean default false;
    declare l_test_key char(32);

    WHILE l_good_key_found = false DO
        select md5(now()) into l_test_key;
        
        select
            if(count(*) > 0, false, true) into l_good_key_found        
        from
            dataset
        where 
            access_key = l_test_key;
    END WHILE;

    select l_test_key into o_dataset_access_key;


END \\
DELIMITER ;