Postgresql 9.x: return custom type from a function -


i'm trying return custom type postgresql function follows:

create type mytype  (      "name" character varying     ,"surname" text  );  create or replace function public.xml_to_type(inputxml xml)   returns setof mytype $body$ declare     xml_content mytype; begin      xml_content in          select          (xpath('//name/text()',testcolumn))[1]::text::character varying "name"         ,(xpath('//surname/text()',testcolumn))[1]::text::text "surname"         unnest(xpath('//mynodes/node',inputxml)) t(testcolumn)     loop      return next xml_content;     end loop;  end; $body$   language plpgsql volatile   cost 100; 

from postgresql query tool works:

select * xml_to_type('<mynodes>         <node>             <name>alex</name>             <surname>red</surname>         </node>         <node>             <name>tony</name>             <surname>stark</surname>         </node>     </mynodes>'::xml); 

and returns correctly 2 records. problem when call function function:

    declare mytypevar mytype[];     select * xml_to_type(xmlexample) mytypevar; 

i exception:

error:  malformed array literal: "alex" detail:  array value must start "{" or dimension information. 

any idea?

a function returns setof x cannot have result stored field of type x[]. sets not arrays.

you must use array_agg:

select  array_agg(x) xml_to_type(xmlexample) x mytypevar; 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -