views:

2500

answers:

4

Is there a direct route that is pretty straight forward? (i.e. can SQL Server read XML)

Or, is it best to parse the XML and just transfer it in the usual way via ADO.Net either as individual rows or perhaps a batch update?

I realize there may be solutions that involve large complex stored procs--while I'm not entirely opposed to this, I tend to prefer to have most of my business logic in the C# code. I have seen a solution using SQLXMLBulkLoad, but it seemed to require fairly complex SQL code.

For reference, I'll be working with about 100 rows at a time with about 50 small pieces of data for each (strings and ints). This will eventually become a daily batch job.

Any code snippets you can provide would be very much appreciated.

+1  A: 

You can use the function OPENXML and stored procedure sp_xml_preparedocument to easily convert your XML into rowsets.

coder
never use sp_xml_preparedocument since SQL Server 2005!!
gbn
why not? Is there a replacement?
alchemical
+1  A: 

SQL Server 2005 and up have a datatype called "XML" which you can store XML in - untyped or typed with a XSD schema.

You can basically fill columns of type XML from an XML literal string, so you can easily just use a normal INSERT statement and fill the XML contents into that field.

Marc

marc_s
Once I use this to get the cml into sql server, will sql server somehow parse the XML out into fields? Or do I need to do this manually in a proc or table?
alchemical
No, it's stored as an XML field - you can query it using XPath and XQuery. What exactly are you looking for?
marc_s
+1  A: 

If you are using SQL Server 2008 (or 2005), it has an xml native datatype. You can associate an XSD schema with xml variables, and Insert directly into columns of type xml.

Mitch Wheat
+1  A: 

Yes, SQL Server 2005 and above can parse XML out of the box.

You use the nodes, value and query methods to break it down how you want, whether values or attributes

Some shameless plugging:

gbn