简体   繁体   English

如何将XML文件导入SQL Server 2012

[英]How to import an XML file into SQL Server 2012

I have an XML file that I want to import into SQL Server 2012 . 我有一个要导入SQL Server 2012的XML文件。 I haven't done it before and I don't know a lot. 我以前没做过,我也不了解。 After some research, I tried to open the xml file with the following command that was run in SQL Server Management Studio. 经过一番研究之后,我尝试使用在SQL Server Management Studio中运行的以下命令打开xml文件。

SELECT CAST(x as XML) FROM OPENROWSET(
BULK 'C:\bulk\Users_test1111.xml',
SINGLE_BLOB
) AS X  

For this I get the following error: 为此,我得到以下错误:

Msg 4861, Level 16, State 1, Line 1
Cannot bulk load because the file "C:\bulk\Users_test1111.xml" could not be opened. Operating system error code 3(The system cannot find the path specified.).

What do you suggest to do to get the file into the database? 您建议怎么做才能将文件放入数据库?

I'm guessing that you're having the same issue that I am. 我猜您正在遇到与我相同的问题。 Some of the commands used to load a file using SQL Server require the file to be on the actual DB server itself. 一些用于使用SQL Server加载文件的命令要求该文件位于实际的DB服务器本身上。 If you're running SQL Server Management Studio from a different machine, then you'll have this issue. 如果从另一台计算机上运行SQL Server Management Studio,则会遇到此问题。

Use SSIS. 使用SSIS。 Create an SSIS package with an XML data source. 使用XML数据源创建SSIS包。 If you can't use/don't have integration services, use UNC file paths to eliminate local/network standpoint issues. 如果您无法使用/没有集成服务,请使用UNC文件路径来消除本地/网络立场问题。

Edit:I failed to exercise google-fu prior to shooting off at the mouth and one of my initial ideas was incorrect. 编辑:我未能在口中射击之前锻炼Google Fu,而我的最初想法之一是不正确的。

Try this, I included some extra columns to show you. 尝试此操作,我包括一些额外的列来向您展示。

The file also needs to be on the server itself (the path needs to be located on the server) 该文件还需要位于服务器本身上(路径必须位于服务器上)

INSERT INTO TestTable(TestId, TestXml, TestText)

Values('1', (  
SELECT * FROM OPENROWSET(
   BULK 'C:\bulk\Users_test1111.xml',
   SINGLE_BLOB) AS x), 'Some Test Text')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM