0

The XML looks like this:

<Attributes>
<ProductAttribute ID="92">
    <ProductAttributeValue>
        <Value>225</Value>
    </ProductAttributeValue>
</ProductAttribute>
<ProductAttribute ID="536">
    <ProductAttributeValue>
        <Value>227</Value>
    </ProductAttributeValue>
</ProductAttribute>

I need

ID  - Value
92  - 225
536 - 227

In Table Format.

Is it possible to get this values directly from a query or stored procedure or Function?

3
  • MS Docs has an article on XML data and SQL Server, which you might find useful. Commented May 23, 2017 at 10:13
  • Did you check the docs? They have an entire chapter on storing and parsing XML, queries, transformations etc. Do you have a specific problem? Commented May 23, 2017 at 10:26
  • I have checked it but unable to buildup query same as i want result in table structure. Commented May 23, 2017 at 10:27

1 Answer 1

1

You could use value() and nodes method for xml

DECLARE @xml XML = N'<Attributes>
<ProductAttribute ID="92">
    <ProductAttributeValue>
        <Value>225</Value>
    </ProductAttributeValue>
</ProductAttribute>
<ProductAttribute ID="536">
    <ProductAttributeValue>
        <Value>227</Value>
    </ProductAttributeValue>
</ProductAttribute>
</Attributes>'

SELECT ID = x.t.value('(./@ID)[1]','int'),
       Value = x.t.value('(./ProductAttributeValue/Value)[1]','int') 
FROM @xml.nodes('/Attributes/ProductAttribute') AS x(t)

Returns

ID  Value
---------
92  225
536 227
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.