function returning a record - Mailing list pgsql-general

From Pascal Polleunus
Subject function returning a record
Date
Msg-id 4031F697.8070900@beeznest.net
Whole thread Raw
Responses Re: function returning a record
List pgsql-general
Hi,

I'm trying to return a RECORD from a function, but when I try to use the
variable I have the following error:
ERROR:  record "r" has no field "id"


Here's an example:

CREATE OR REPLACE FUNCTION test()
RETURNS CHARACTER VARYING AS '
DECLARE r RECORD;
BEGIN
     SELECT INTO r get_id(''mytable'');

     RETURN r.id::TEXT || '' : '' || r.name;
END;' LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION get_id(CHARACTER VARYING)
RETURNS RECORD AS '
DECLARE
     mytable ALIAS FOR $1;
     r RECORD;
BEGIN
     FOR r IN EXECUTE
         ''SELECT id, name FROM '' || mytable || '' WHERE id = 1''
     LOOP
         RAISE NOTICE ''r: %, %'', r.id, r.name;
         RETURN r;
     END LOOP;

     RETURN NULL;
END;' LANGUAGE 'plpgsql';


test=> select test();
NOTICE:  r: 1, ttt
CONTEXT:  PL/pgSQL function "test" line 3 at select into variables
ERROR:  record "r" has no field "id"
CONTEXT:  PL/pgSQL function "test" line 5 at return


Any idea welcomed ;-)


Thanks,
Pascal



pgsql-general by date:

Previous
From: "John Sidney-Woollett"
Date:
Subject: Re: How do I call multiple functions in one trigger?
Next
From: "Matthew Lunnon"
Date:
Subject: summary aggregate information from a second table