游标类型

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

  • 复制
    复制成功
postgres=# CREATE OR REPLACE FUNCTION f5 (a_ref refcursor) RETURNS void AS
postgres-# $$
postgres$# DECLARE
postgres$#     v_rec record;
postgres$# BEGIN
postgres$#     OPEN a_ref FOR SELECT * FROM t LIMIT 1;
postgres$#     FETCH a_ref INTO v_rec;
postgres$#     RAISE NOTICE 'v_rec = % ',v_rec;
postgres$# END;
postgres$# $$
postgres-# LANGUAGE PLPGSQL;
CREATE FUNCTION
postgres=#
postgres=# SELECT * FROM f5('a');
NOTICE:  v_rec = (1,TDSQL PG)
 f5
----
(1 行记录)
postgres=# CREATE OR REPLACE FUNCTION f6 (a_ref refcursor) RETURNS refcursor AS
postgres-# $$
postgres$# BEGIN
postgres$#     OPEN a_ref FOR SELECT * FROM t LIMIT 1;
postgres$#     RETURN a_ref;
postgres$# END;
postgres$# $$
postgres-# LANGUAGE PLPGSQL;
CREATE FUNCTION
postgres=#
#注意这里需要开启一个事务
postgres=# BEGIN;
BEGIN
postgres=# SELECT * FROM f6('a');
 f6
----
 a
(1 行记录)
postgres=# FETCH ALL FROM a;
 id |  mc
----+------
  1 | TDSQL PG
(1 行记录)