返回一个游标

最近更新时间: 2025-02-18 16:02:00

  • 复制
    复制成功
postgres=# CREATE OR REPLACE FUNCTION f16() RETURNS refcursor AS
postgres-# $$
postgres$# DECLARE
postgres$#     v_ref refcursor;
postgres$# BEGIN
postgres$#     OPEN v_ref FOR SELECT * FROM public.t;
postgres$#     RETURN v_ref;
postgres$# END;
postgres$# $$
postgres-# LANGUAGE plpgsql;
CREATE FUNCTION
postgres=#
postgres=# BEGIN;
BEGIN
postgres=#
postgres=# SELECT * FROM f16();
        f15
--------------------
 <unnamed portal 1>
(1 行记录)
postgres=# FETCH ALL FROM "<unnamed portal 1>";
 id |   mc
----+--------
  1 | TDSQL PG
  2 | pgxz
(2 行记录)
postgres=# END;
postgres=# CREATE OR REPLACE FUNCTION f16(a_ref refcursor) RETURNS refcursor AS
postgres-# $$
postgres$# BEGIN
postgres$#     OPEN a_ref FOR SELECT * FROM public.t;
postgres$#     RETURN a_ref;
postgres$# END;
postgres$# $$
postgres-# LANGUAGE plpgsql;
CREATE FUNCTION
postgres=#
postgres=# BEGIN;
BEGIN
postgres=# SELECT * FROM f16('a');
 f15
-----
 a
(1 行记录)
postgres=# FETCH ALL FROM a;
 id |   mc
----+--------
  1 | TDSQL PG
  2 | pgxz
(2 行记录)
postgres=# END;
COMMIT