Calling a function that returns a refcursor

后端 未结 2 1410
陌清茗
陌清茗 2020-12-10 03:53

I am using Postgresql 8.3 and have the following simple function that will return a refcursor to the client

CREATE OR REPLACE FUNCTION functio         


        
2条回答
  •  盖世英雄少女心
    2020-12-10 04:36

    I'm not quite sure from wich version of Postgre this is available (in 8.4 it is valid) but i found quite easiest to define the cursor name when you declare it, like this:

    CREATE OR REPLACE FUNCTION function_1() RETURNS refcursor AS $$
    DECLARE
            ref_cursor REFCURSOR := 'mycursor';
    BEGIN
            OPEN ref_cursor FOR SELECT * FROM some_table;
            RETURN (ref_cursor);    
    END;
    $$ LANGUAGE plpgsql;
    

    And then you can get it like this:

    BEGIN;
    SELECT function_1();
    FETCH 4   from  mycursor; 
    COMMIT;
    

    I find this method less cumbersome. Hope that helps.

提交回复
热议问题