This code snippet shows how to work with data stored in xml variable (or column of type xml). The xml data are joined with table and there are joined some data to output.
-- input
set @xml = '<docs>
<doc> <DocId>1000052</DocId><InvoiceNumber>S37</InvoiceNumber></doc>
<doc> <DocId>1000053</DocId><InvoiceNumber>S74</InvoiceNumber></doc>
<doc> <DocId>1000054</DocId><InvoiceNumber>E85</InvoiceNumber></doc>
</docs>'
SELECT
p.value('(DocId)[1]', 'VARCHAR(10)') AS DocId,
p.value('(InvoiceNumber)[1]', 'VARCHAR(100)') AS InvoiceNumber,
ISNULL(d.SupplierID, '') AS Supplier
FROM @xml.nodes('/docs/doc') doc(p)
INNER JOIN Documents d ON d.DocId = p.value('(DocId)[1]', 'VARCHAR(10)')
FOR XML AUTO, ROOT ('docs')
Output:
<docs>
<doc DocId="1000052" InvoiceNumber="S37" Supplier="Microsoft" />
<doc DocId="1000053" InvoiceNumber="S74" Supplier="Amazon" />
<doc DocId="1000054" InvoiceNumber="E85" Supplier="Sony" />
</docs>
Resources:
No comments:
Post a Comment