I am using SQL Server 2008.
Task: take the XML file and parse it in the (n) SQL table.
Problem: the number of columns and their names will vary depending on the XML.
Here is the code:
DECLARE @xmlData XML; SET @xmlData = '<root> <item id="1"> <item_number>IT23</item_number> <title>Item number twenty-three</title> <setting>5 to 20</setting> <parameter>10 to 16</parameter> </item> <item id="2"> <item_number>RJ12</item_number> <title>Another item with a 12</title> <setting>7 to 35</setting> <parameter>1 to 34</parameter> </item> <item id="3"> <item_number>LN90</item_number> <title>LN with 90</title> <setting>3 to 35</setting> <parameter>9 to 50</parameter> </item> </root>'
For example, using the XML above, I will need a returned SQL table that looks like this:

This is how I got the table above:
DECLARE @idoc INT; EXEC sp_xml_preparedocument @idoc OUTPUT, @xmlData SELECT * FROM OPENXML (@idoc, '/root/item', 2) WITH (item_number VARCHAR(100), title VARCHAR(100), setting VARCHAR(100), parameter VARCHAR(100))
Now let's say that the XML has changed where each node element has a new node child named "new_node". For instance:
<root> <item id="1"> <item_number>IT23</item_number> <title>Item number twenty-three</title> <setting>5 to 20</setting> <parameter>10 to 16</parameter> <new_node>data</new_node> </item> <item id="2"> <item_number>RJ12</item_number> <title>Another item with a 12</title> <setting>7 to 35</setting> <parameter>1 to 34</parameter> <new_node>goes</new_node> </item> <item id="3"> <item_number>LN90</item_number> <title>LN with 90</title> <setting>3 to 35</setting> <parameter>9 to 50</parameter> <new_node>here</new_node> </item> </root>
I have to change my code to include a new node:
SELECT * FROM OPENXML (@idoc, '/root/item', 2) WITH (item_number VARCHAR(100), title VARCHAR(100), setting VARCHAR(100), parameter VARCHAR(100), new_node VARCHAR(100))
To get this table:

Thus, the problem is that the child nodes of the "item" will change.
How can I generate the same tables without specifying columns? Is there any other approach than using OPENXML?