I have an XML structure like the following:
<tables>
<table name="tableName1">
<row ID="34" col1="data" col2="dom" />
<row ID="35" col1="data2" col2="dom2" />
</table>
<table name="tableName2">
<row ID="1" col1="data" col2="dom" col3="item1" />
<row ID="3" col1="data2" col2="dom2" col3="item2" />
<row ID="7" col1="data4" col3="item3" />
</table>
...
<tables>
Basically the table nodes contain RAW data created by selecting FOR XML RAW.
Now I wish to do the reverse: read the XML and insert data into respective tables of a SQL Server 2008 R2 database. However I want the loading process to be robust, meaning I do not want to mess with column names and table names if they change in the future. I need the process to read table names from @name attributes of table nodes and insert data into columns specified by attributes in <Row> nodes. I thought of a stored procedure that gets an XML as input and does the rest.
The amount of data is approx. 70 tables ranging from 10 to 30 000 rows, altogether no more than 100 000 rows. I need to do it as efficiently as possible, bulk loading would be the best.
The process should not take care of foreign keys as the order of tables inside the XML is built so that FK constraints can be kept in place by loading one table after the other.
However there are identity columns in each table so I must do a
SET Identity_Insert ON and SET Identity_Insert OFF
before and after processing each table. I also need to reseed each table after inserting all rows. Oh,and I need to do the whole shebang in a transaction so that I could roll back if something goes wrong.
Which way do you suggest I go: should I stay with T-SQL or try to write the SP in CLR SQL? Should I use XQuery or can I use some bulk insert method?
Thanks for all the help!
As you are dealing with fairly big XML documents, I recommend at this point to use a .net shredder. You can do that in a CLR procedure or an external tool. You could also use the build in xquery of SQL Server, but that will be slow.
However, looking at this and your previous question (Dump data into single XML file from MS SQL Server 2008 R2), I am thinking you might be better of using something like the BCP utility or even replication. What are your exact requirements?