January 1, 2011 at 11:44 am
Comments posted to this topic are about the item Loading XML Data into SQL Server (SQL Spackle)
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
January 3, 2011 at 12:49 am
I have to admit, I've not yet had to enter the realm of loading XML files. But now I know where to go for a quick reference. Thanks for filling in the cracks on this one, Wayne!
--Jeff Moden
Change is inevitable... Change for the better is not.
January 3, 2011 at 1:48 am
WayneS (1/1/2011)
If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
It's a nice article. Can you explain why do you use the OpenRowset function to load the data into a staging table?
Kelsey Thornton
MBCS CITP
January 3, 2011 at 3:28 am
I wonder what others are doing about doing an example like this when the XML is from a web service. I run into that more often then XML files. I tend to write C# as a separate application, but are db pros accessing plain old XML web services in the CLR of SQL Server. I would like to see an example like that.
January 3, 2011 at 7:46 am
Kelsey Thornton (1/3/2011)
WayneS (1/1/2011)
If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!It's a nice article. Can you explain why do you use the OpenRowset function to load the data into a staging table?
Hi Kelsey,
Very good question. (I especially like the quote from my signature... :-)) In retrospect, I think I should have included this in the article. Thanks for bringing it up!
To answer your question - it's to load the entire XML file into one record into a table, where we can then work with it. The code in reference is:
OPENROWSET(BULK N'C:\SQL\cd_catalog.xml', SINGLE_BLOB)
Notice that we're using the "BULK" and "SINGLE_BLOB" options of OPENROWSET. From the OpenRowset BOL reference in the article, here is what these two options do:
BULK
Uses the BULK rowset provider for OPENROWSET to read data from a file.
SINGLE_BLOB
Returns the contents of data_file as a single-row, single-column rowset of type varbinary(max).
I'm sure that there are other ways to accomplish this (for instance, utilize the sp_OA procedures to start up Scripting.FileSystemObject, open the file, read it line by line, append what you just read into a variable, and when finished reading the file, close it and write the results to a table - and clean up all the objects that you had to create along the way). Currently, I believe that using OpenRowset with the BULK and SINGLE_BLOB options is going to be the easiest, and probably the fastest way.
A discussion of all the various ways to load an external file into a table would lead to an article of it's own - beyond the scope of this "SQL Spackle"! Therefore, I just used the quick/easy way to get the job done.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
January 3, 2011 at 8:10 am
SQL-DBA (1/3/2011)
I wonder what others are doing about doing an example like this when the XML is from a web service. I run into that more often then XML files. I tend to write C# as a separate application, but are db pros accessing plain old XML web services in the CLR of SQL Server. I would like to see an example like that.
I think that this link will give you an example.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
January 3, 2011 at 8:21 am
Thanks for this.
It's now clearer.
Kelsey Thornton
MBCS CITP
January 3, 2011 at 8:42 am
decent article - good reference. but I feel a comment about the bcp utility should be added for those situations that warrant a different approach.
January 3, 2011 at 8:45 am
Excellent discussion, Wayne. This is one that I will keep bookmarked.
January 3, 2011 at 9:16 am
I'm getting an the following error when I try to run the OPENROWSET function:
DECLARE @CD TABLE (XMLData XML);
INSERT INTO @CD
SELECT *
FROM OPENROWSET(BULK N'C:\SQL\cd_catalog.xml', SINGLE_BlOB)
Msg 491, Level 16, State 1, Line 5
A correlation name must be specified for the bulk rowset in the from clause.
Is there something in the code beyond SINGLE_BLOB) ?
Thanks
G. Daniels
January 3, 2011 at 9:57 am
gregg_dn (1/3/2011)
I'm getting an the following error when I try to run the OPENROWSET function:DECLARE @CD TABLE (XMLData XML);
INSERT INTO @CD
SELECT *
FROM OPENROWSET(BULK N'C:\SQL\cd_catalog.xml', SINGLE_BlOB)
Msg 491, Level 16, State 1, Line 5
A correlation name must be specified for the bulk rowset in the from clause.
Is there something in the code beyond SINGLE_BLOB) ?
Thanks
G. Daniels
Yes, as the author noted, you have to provide an alias to the OpenRowset. In his example the author used rs. This will get you past that particular error. Good luck.
January 3, 2011 at 10:06 am
Excellent Wayne, thank you sir!
:smooooth:
The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge. - Stephen Hawking
January 3, 2011 at 10:08 am
Thanks!
January 3, 2011 at 11:55 am
Kelsey Thornton (1/3/2011)
Thanks for this.It's now clearer.
Kelsey,
Good, I'm glad it all makes sense. If it hadn't, I would have failed.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
January 3, 2011 at 11:58 am
paul.marsh (1/3/2011)
decent article - good reference. but I feel a comment about the bcp utility should be added for those situations that warrant a different approach.
Paul - thanks. But how would you use BCP to load an XML file in? (This is what this article is about...)
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
Viewing 15 posts - 1 through 15 (of 60 total)
You must be logged in to reply to this topic. Login to reply