我有一个表(TableA(,我从它的2个字段(Fld1和Fld2(创建一个XML。
select
XMLSERIALIZE(
XMLAGG(
XmlElement(Name "Tag",
XmlElement(Name "PARTNUMBER", RTrim(Fld1)),
XmlElement(Name "PART_DESCRIPTION", RTrim(Fld2)))
)
AS varchar(2000) )
From TableA
输出如下:
<Tag><PARTNUMBER>401791</PARTNUMBER><PART_DESCRIPTION>ECM-NO START,ME17</PART_DESCRIPTION></Tag><Tag><PARTNUMBER>401792</PARTNUMBER><PART_DESCRIPTION>CALIBRATION-111,IND,18,NA</PART_DESCRIPTION></Tag>
现在我的要求是,应该只来一次。它应该绑定整个 XML,而不是每一行。
所需的输出应如下所示:
<Tag><PARTNUMBER>401791</PARTNUMBER><PART_DESCRIPTION>ECM-NO> START,ME17</PART_DESCRIPTION><PARTNUMBER>401792</PARTNUMBER><PART_DESCRIPTION>CALIBRATION-111,IND,18,NA</PART_DESCRIPTION></Tag>
请帮忙!
对于您请求的输出,这应该可以解决问题...
SELECT
XMLELEMENT(NAME "TAG",
XMLAGG(
XMLFOREST(RTRIM(Fld1) AS "PARTNUMBER",
RTRIM(Fld2) AS "PART_DESCRIPTION"
)
)
)
AS XML_EXAMPLE
FROM TableA
现在,在生成它之后,你可以把它扔到另一个选择语句中,并使用XMLSERIALIZE在开始时添加你需要的任何内容。
此外,下面是上面使用的三个 XML 函数的 IBM 文档。
XMLELEMENT: https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_72/db2/rbafzscaxmlelem.htm
XMLAGG: https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_72/db2/rbafzcolxmlagg.htm
XMLFOREST: https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_72/db2/rbafzscaxmlforest.htm