I am trying to import XML data into SQL server. I found below link.
My requirement is slight different, where I need to create the table with <name>
as column name and <value>
as column values. like below
XML:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:toatech:ResourceManagement:1.0">
<SOAP-ENV:Body>
<ns1:get_resources_list_response>
<result_code>0</result_code>
<resources>
<resource>
<properties>
<property>
<name>id</name>
<value>CAP_LOAD_TEST</value>
</property>
<property>
<name>status</name>
<value>inactive</value>
</property>
<property>
<name>parent_id</name>
<value>ABC_CO</value>
</property>
<property>
<name>type</name>
<value>GR</value>
</property>
<property>
<name>name</name>
<value>Capacity LoadTesting</value>
.
.
.
.
Code:
DECLARE @XmlFile XML
SELECT @XmlFile = BulkColumn
FROM OPENROWSET(BULK 'C:\Users\GET_RESOURCE_LIST.xml', SINGLE_BLOB) x
SELECT
Resource_id = resource.value('(value)[1]', 'varchar(255)'),
Resource_status = resource.value('(value)[2]', 'varchar(255)')
FROM
@XmlFile.nodes('//resources/resource/properties/property') AS XTbl1(resource)
So when "resource" item repeats it should be a new row in the table with new set of values
Expected Table Output:
id status parent_id type name
== ====== ========= ==== ====
CAP_LOAD_TEST inactive ABC_CO GR Capacity LoadTesting
...
With the above code all the values populating to one column. That was the issue.