how to read the content of xml file in the remote(network) machine through mssql stored procedures

五迷三道 提交于 2019-12-31 03:54:07

问题


I tried to read the contents of single xml file in the local machine using bulk insert.

SELECT * FROM OPENROWSET ( BULK '''+ @FILENAME+''' , SINGLE_CLOB ) AS xmlData

It is working, but the same i am trying to read from the remote machine. its giving the following error

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

i given the filepath as

\\172.16.11.52\D:\TechnicalLoss_EnergyAccounting_10.10.2012.12.19.PM.XML

It gives the same error then, I created the network share drive for that remote machine that is (z:) given the filepath as

Z:\TechnicalLoss_EnergyAccounting_10.10.2012.12.19.PM.XML

same error :-( Help me to resolve it?


回答1:


The issue here seems to be about file shares, not about SQL Server.

First, a drive mapping is per-user, so using drive letters causes problems when working with SQL Server because the SQL Server service account doesn't have any drive letters mapped. For that reason it's best to completely avoid drive letters and simply use UNC paths.

Next, the form of a share is \\ServerName\Share. It looks like the file you're trying to access is in the root of the D: drive, so the correct path would be \\172.16.11.52\D$\TechnicalLoss_EnergyAccounting_10.10.2012.12.19.PM.XML. But, note that D$ is a default admin share and only administrators can use it. Since the SQL Server service account hopefully does not have admin rights, you shouldn't be using it anyway, and storing files in the root of any drive (especially C:) is usually a bad practice.

Finally, even if you have a share, the account you use to access the share needs to have permissions both on the share and on the filesystem. This is one benefit to running SQL Server using a domain account.

In summary, what you probably need to do is this:

  1. If it isn't already, run SQL Server using a domain account (see link above)
  2. Create a folder for your files, call it D:\XMLFiles (or whatever you like)
  3. Share the folder, so now you have a share called \\172.16.11.52\XMLFiles
  4. Ensure that the SQL Server service account has permissions to read from the share
  5. Try your code again:

SELECT * FROM OPENROWSET ( BULK '\\172.16.11.52\XMLFiles\TechnicalLoss_EnergyAccounting_10.10.2012.12.19.PM.XML' , SINGLE_CLOB ) AS xmlData



来源:https://stackoverflow.com/questions/12835206/how-to-read-the-content-of-xml-file-in-the-remotenetwork-machine-through-mssql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!