以编程方式将XML数据导入MS SQL Server

Jon*_*ley 9 xml sql sql-server

我有5个大型XML文件,我很想分析.所有这些都太大了,无法在文本编辑器中打开,所以我不知道他们的XML模式.

我试图将它们导入SQL服务器,但是这个过程给了我一个错误,即使我很确定它们是有效的,因为它们来自非常有信誉的程序员.

我也尝试了其他方法,但每个方法都在使用大文件大小(MySQL),或者声明文件包含无效的XML字符(Access和Excel).

如何以编程方式读取和插入数据?这可以通过SQL查询完成吗?

非常感谢!

Dan*_*n S 5

As of 2013...

The only time saving option in my opinion to load large/huge XML files in SQL Server is (as someone previously briefly mentioned) to use the SQLXML 4.0 library.

This is the solution I adopted to load huge XML files (7GB in size) on a daily basis. The previous process which was using C# manipulation in the Script Task took hours to complete. Using SQLXML 4.0 takes 15-20 minutes. How to install SQLXML 4.0. step by step here. For practical examples in how to do it end to end follow this MSDN link.

My XML has also nested elements, so it's quite complex, the result is 10 tables with 2.5 to 4 million rows each (the daily file sometimes is more than 7GB). My work was based purely on information I learned and applied from the two links provided above.

  • Advantages:

    • it's fast
    • it's Microsoft (http://www.microsoft.com/en-gb/download/details.aspx?id=30403)
    • SSIS package will be very much simplified
    • you don't need to spend hours and hours to change the SSIS package if your XML schema changes. SQLXML is able to create the tables in the SQL Server for you every time you run the package, based on the XSD relationships you provide.
  • Disadvantages

    • creating the XSD may take a while and requires some knowledge. When I did it I learned something new, so this was not a real a disadvantage for me.
    • when seeing how simple the SSIS package is, your manager will have the impression that you didn't do any work.

To view large files use Large Text File Viewer, nice little gem.

Note: The question is quite old, but the "issue" remains hot. I added this post for the developers who Google how to BULK LOAD XML files in SSIS and land here.


Joe*_*orn 3

请参阅非官方 StackOverflow 团队成员 Brent Ozar 撰写的博客文章:
http://www.brentozar.com/archive/2009/06/how-to-import-the-stackoverflow-xml-into-sql-server/

  • “2013 年更新:此代码不再可用。StackOverflow 导出的大小已超出您可以使用此方法导入的大小”。值得一提。 (2认同)