Testing PostgreSQL functions that consume and return refcursor

牧云@^-^@ 提交于 2019-11-30 20:21:41

Q1

Your "small test" can be plain SQL:

BEGIN;
SELECT get_function_that_returns_cursor('ret', 4100, 'foo', 123); -- note: 'ret'
FETCH ALL IN ret; -- works for any rowtype

COMMIT;  -- or ROLLBACK;

Execute COMMIT / ROLLBACK after you inspected the results. Most clients only display the result of the lat command.

More in the chapter Returning Cursors of the manual.

Q2

And if we don't know the rowtype that is being returned how could we do it?

Since you only want to inspect the results, you could cast the whole record to text. This way you avoid the problem with dynamic return types for the function altogether.

Consider this demo:

CREATE TABLE a (a_id int PRIMARY KEY, a text);
INSERT INTO a VALUES (1, 'foo'), (2, 'bar');

CREATE OR REPLACE FUNCTION reffunc(INOUT ret refcursor) AS  -- INOUT param :)
$func$
BEGIN
    OPEN ret FOR SELECT * FROM a;
END
$func$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION ctest()
  RETURNS SETOF text AS
$func$
DECLARE
    curs1 refcursor;
    rec   record;
BEGIN
  curs1 := reffunc('ret');   -- simple assignment

  LOOP
    FETCH curs1 INTO rec;
    EXIT WHEN NOT FOUND;     -- note the placement!
    RETURN NEXT rec::text;
  END LOOP;
END
$func$ LANGUAGE plpgsql;

-> SQLfiddle

This worked for what I wanted:

DO $$ DECLARE
 mycursor REFCURSOR;
 rec RECORD;
BEGIN
 SELECT 'ret' INTO mycursor FROM get_function_that_returns_cursor('ret'::REFCURSOR, 4100, 'SOMETHING', 123465);
 WHILE (FOUND) LOOP
   FETCH mycursor INTO rec;
   RAISE NOTICE 'Row read. Data: % ', rec.collumn_name;
 END LOOP;
END $$
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!