returning ref cursor

From: Ravi Katkar <Ravi(dot)Katkar(at)infor(dot)com>
To: "'pgsql-general(at)postgresql(dot)org'" <pgsql-general(at)postgresql(dot)org>
Subject: returning ref cursor
Date: 2010-05-24 05:44:38
Message-ID: 46AC8D44F3AE1F4888F137837D0DF635072561999A@INHYWEXMB2.infor.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Please let me know what's wrong with below code

CREATE LANGUAGE plpgsql;
create or replace FUNCTION test_cu( p_cursor REFCURSOR) returns REFCURSOR
AS $procedure$
BEGIN
open p_cursor FOR
select * from test;

RETURN p_cursor;
END; $procedure$
LANGUAGE plpgsql;

create or replace FUNCTION test_call()
RETURNS VOID
AS $procedure$
DECLARE
c_cursor REFCURSOR;
r_emp test%rowtype;
BEGIN
PERFORM test_cu(c_cursor);
loop
fetch c_cursor into r_emp;
exit when NOT FOUND;
RAISE NOTICE '%',r_emp.aaa;
end loop;
close c_cursor;
RETURN;
END; $procedure$
LANGUAGE plpgsql;

SELECT test_call();

When I execute above code I got below error

ERROR: cursor variable "c_cursor" is null
CONTEXT: PL/pgSQL function "test_call" line 7 at FETCH

********** Error **********

ERROR: cursor variable "c_cursor" is null
SQL state: 22004
Context: PL/pgSQL function "test_call" line 7 at FETCH

Thanks,
Ravi Katkar

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Ivan Voras 2010-05-24 09:41:37 Re: Full text search on a complex schema - a classic problem?
Previous Message rihad 2010-05-24 04:39:37 Re: UPDATE ... RETURNING atomicity