CREATE OR REPLACE FUNCTION cursor_demo() RETURNS refcursor AS $BODY$ DECLARE cur CURSOR FOR select id , totalspace from aaa where discardflag = '0'; v_id int; v_totalspace int;begin open cur; FETCH cur into v_id,v_totalspace ; while found loop insert into a values (v_id,v_totalspace); FETCH next from cur into v_id,v_totalspace ; end loop; RETURN cur ; close cur;END;$BODY$ LANGUAGE plpgsql;