问题
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:
- If it isn't already, run SQL Server using a domain account (see link above)
- Create a folder for your files, call it
D:\XMLFiles
(or whatever you like) - Share the folder, so now you have a share called
\\172.16.11.52\XMLFiles
- Ensure that the SQL Server service account has permissions to read from the share
- 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