游标使用限制

最近更新时间: 2024-06-12 15:06:00

postgres=# create table t_ref(f1 int,f2 int);
CREATE TABLE
postgres=# insert into t_ref values(1,1),(2,2),(3,3);
COPY 3
postgres=# CREATE OR REPLACE procedure p_refcursor() AS
$$
DECLARE
    v_ref refcursor;
v_rec record;
BEGIN
    OPEN v_ref FOR SELECT * FROM t_ref;
    fetch next from v_ref into v_rec ;
commit;
fetch next from v_ref into v_rec ;
END;
$$
LANGUAGE plpgsql;
CREATE PROCEDURE
postgres=# call p_refcursor();
ERROR:  cursor "<unnamed portal 5>" does not exist
CONTEXT:  PL/pgSQL function p_refcursor() line 9 at FETCH

在存储过程中如果使用了游标,则游标在commit后不可使用。