1

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

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;

有没有办法让后者工作?

4

1 回答 1

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;
于 2013-02-12T06:54:00.963 回答