在上下文中调用的Postgresql coalesce和set-valued函数不能接受set

时间:2013-02-12 06:47:57

标签: xml postgresql coalesce

我有这两个类似的查询但在第一种情况下它可以正常工作但在第二种情况下失败

CREATE OR REPLACE FUNCTION XmlNodes(xmlData xml, selector TEXT) RETURNS  TABLE(r xml)
AS $$ SELECT unnest(xpath(selector, xmlData)); $$ LANGUAGE SQL;

with tmp (x) AS(
SELECT r from
 XmlNodes('<Data>
  <RegDeletedItem Id="a60ded3d-2d2f-4f57-91d5-0091579bddb9" />
  <RegDeletedItem Id="4295e41c-0a09-4601-984a-eac7a9e91fe1" />
</Data>', '/Data/RegDeletedItem')),
sel (y) AS (
select CAST(unnest(xpath('/RegDeletedItem/@Id', r.x))::varchar AS uuid) AS "DeletedItem_ForeignEntity" from tmp as r)


SELECT COALESCE(y, '00000000-0000-0000-0000-000000000000') AS "DeletedItem_ForeignEntity" from sel

失败了

错误:在上下文中调用的集值函数不能接受集合

SELECT coalesce((CAST(unnest(xpath('/RegDeletedItem/@Id', r))::varchar AS uuid)), '00000000-0000-0000-0000-000000000000') AS "DeletedItem_ForeignEntity" from
 XmlNodes('<Data>
  <RegDeletedItem Id="a60ded3d-2d2f-4f57-91d5-0091579bddb9" />
  <RegDeletedItem Id="4295e41c-0a09-4601-984a-eac7a9e91fe1" />
</Data>', '/Data/RegDeletedItem') as r;

有没有办法让后者奏效?

1 个答案:

答案 0 :(得分:0)

好的,我找到了方法,请注意来自xpath的SELECT:

SELECT coalesce((CAST((SELECT x[1]::varchar FROM xpath('/RegDeletedItem/@Id', r) AS x) AS uuid)), '00000000-0000-0000-0000-000000000000') AS "DeletedItem_ForeignEntity" from
 XmlNodes('<Data>
  <RegDeletedItem Id="a60ded3d-2d2f-4f57-91d5-0091579bddb9" />
  <RegDeletedItem Id="4295e41c-0a09-4601-984a-eac7a9e91fe1" />
</Data>
', '/Data/RegDeletedItem') as r;