我正在尝试从PostgreSQL函数返回一个Custom类型,如下所示:
CREATE TYPE myType as
(
"Name" character varying
,"Surname" Text
);
CREATE OR REPLACE FUNCTION public.xml_to_type(inputXml xml)
RETURNS SETOF myType AS
$BODY$
DECLARE
xml_content myType;
BEGIN
FOR xml_content in
SELECT
(xpath('//Name/text()',testColumn))[1]::text::character varying as "Name"
,(xpath('//Surname/text()',testColumn))[1]::text::text as "Surname"
FROM unnest(xpath('//myNodes/node',inputXml)) AS t(testColumn)
LOOP
RETURN NEXT xml_content;
END LOOP;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
从PostgreSQL查询工具开始工作:
SELECT * FROM xml_to_type('<myNodes>
<node>
<Name>Alex</Name>
<Surname>Red</Surname>
</node>
<node>
<Name>Tony</Name>
<Surname>Stark</Surname>
</node>
</myNodes>'::xml);
并正确返回2条记录。问题是当我从另一个函数调用此函数时:
DECLARE myTypeVar myType[];
SELECT * FROM xml_to_type(xmlExample) INTO myTypeVar;
我得到了这个例外:
ERROR: malformed array literal: "Alex"
DETAIL: Array value must start with "{" or dimension information.
有什么想法吗?
答案 0 :(得分:0)
返回SETOF x
的函数不能将其结果存储到x[]
类型的字段中。集合不是数组。
您必须使用array_agg
:
SELECT array_agg(x) FROM xml_to_type(xmlExample) x INTO myTypeVar;