1

I needed to turn this xml from this:

          <Row>
            <Columns>
              <Column>
                <Value>BB1</Value>
                <Name>Location</Name>
              </Column>
              <Column>
                <Value>1593338</Value>
                <Name>Location_Key</Name>
              </Column>
              <Column>
                <Value>0</Value>
                <Name>Quantity</Name>
              </Column>
            </Columns>
          </Row>
          <Row>
            <Columns>
              <Column>
                <Value>DR11 40</Value>
                <Name>Location</Name>
              </Column>
              <Column>
                <Value>1593251</Value>
                <Name>Location_Key</Name>
              </Column>
              <Column>
                <Value>0</Value>
                <Name>Quantity</Name>
              </Column>
            </Columns>
          </Row>

To this table with these columns and rows

(Location)  (Location_Key)  (Quantity)
BB1         1593338            0 
DR11        1593251            0

Using Sql Server

I have been trying for weeks but to no avail. Any answer would be greatly aprreciated.

2
  • 1
    What version of SQL Server are you using? Commented Jan 2, 2014 at 17:39
  • Does the table already exist in your database or are you trying to create the table automatically, then add rows to it? Commented Jan 2, 2014 at 18:14

1 Answer 1

2

Try this:

DECLARE @input XML = '<Row>
            <Columns>
              <Column>
                <Value>BB1</Value>
                <Name>Location</Name>
              </Column>
              <Column>
                <Value>1593338</Value>
                <Name>Location_Key</Name>
              </Column>
              <Column>
                <Value>0</Value>
                <Name>Quantity</Name>
              </Column>
            </Columns>
          </Row>
          <Row>
            <Columns>
              <Column>
                <Value>DR11 40</Value>
                <Name>Location</Name>
              </Column>
              <Column>
                <Value>1593251</Value>
                <Name>Location_Key</Name>
              </Column>
              <Column>
                <Value>0</Value>
                <Name>Quantity</Name>
              </Column>
            </Columns>
          </Row>'

SELECT
    Location = xc.value('(Column[Name="Location"]/Value)[1]', 'varchar(20)'),
    LocationKey = xc.value('(Column[Name="Location_Key"]/Value)[1]', 'varchar(20)'),
    Quantity = xc.value('(Column[Name="Quantity"]/Value)[1]', 'int')
FROM 
    @input.nodes('/Row/Columns') AS XT(XC)

This gives you an output something like this:

enter image description here

You can use this SELECT inside an INSERT INTO ..... or do whatever you like to do with it.

Sign up to request clarification or add additional context in comments.

1 Comment

I also needed to know how/where to declare the namespaces:<br> <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <soap:Body> <ExecuteDataSourceResponse xmlns="http://www.plexus-online.com/DataSource">

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.