September 3, 2008 at 9:06 am
How can I import XML from a file into an XML field in a SQL Server 2005 table?
September 3, 2008 at 9:16 am
Try something like this...
DECLARE @FileName VARCHAR(150)
SET @FileName = 'C:\file.xml'
CREATE TABLE #XmlImport
(
xmlFileName VARCHAR(300),
xml_data xml
)
-- dynamic sql is just so we can use @FileName variable in OPENROWSET
EXEC ( '
INSERT INTO #XmlImport(xmlFileName, xml_data)
SELECT ''' + @FileName + ''', xmlData
FROM
(SELECT *
FROM OPENROWSET (BULK ''' + @FileName + ''' , SINGLE_BLOB) AS XMLDATA
)AS FileImport (XMLDATA)
' )
September 3, 2008 at 9:17 am
September 3, 2008 at 9:48 am
Thanks for the reply. I've been trying this approach, but it seems like each way I try, it says the xml file does no exist when I have the exact path.
September 3, 2008 at 10:10 am
September 3, 2008 at 10:12 am
In order to make sure I wasn't making a typo I pretty much copied and pasted your code:
DECLARE @FileName VARCHAR(500)
SET @FileName = 'C:\parcels_poly_center.xml'
CREATE TABLE #XmlImport
(
xmlFileName VARCHAR(500),
xml_data xml
)
-- dynamic sql is just so we can use @FileName variable in OPENROWSET
EXEC ( '
INSERT INTO #XmlImport(xmlFileName, xml_data)
SELECT ''' + @FileName + ''', xmlData
FROM
(SELECT *
FROM OPENROWSET (BULK ''' + @FileName + ''' , SINGLE_BLOB) AS XMLDATA
)AS FileImport (XMLDATA)
' )
And the error is:
Msg 4860, Level 16, State 1, Line 2
Cannot bulk load. The file "C:\parcels_poly_center.xml" does not exist.
September 3, 2008 at 10:20 am
I don't know what account OPENROWSET from SQL Server runs under, but you should make sure that it has the necessary access to that directory and file.
[font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
Proactive Performance Solutions, Inc. [/font][font="Verdana"] "Performance is our middle name."[/font]
September 3, 2008 at 10:53 am
If I'm logged into SQL Server Management Studio using Windows Authentication, shouldn't SQL Server use my account?
September 3, 2008 at 11:08 am
GibsonM111 (9/3/2008)
If I'm logged into SQL Server Management Studio using Windows Authentication, shouldn't SQL Server use my account?
Not necessarily. The actual security context that SQL Server uses for external facilities is different for every facility. I do not know what OPENROWSET to SQLNCLI uses, but most of them do not use your Windows security context.
If it is like Linked Servers, then it might be trying to impersonate your Windows context, but it might be using something else entirely.
[font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
Proactive Performance Solutions, Inc. [/font][font="Verdana"] "Performance is our middle name."[/font]
September 3, 2008 at 11:59 am
This seemed to have worked:
EXEC('
INSERT INTO mapData_center(sourceXML)
SELECT *
FROM OPENROWSET(BULK ''C:\parcels_poly_center.xml'', SINGLE_CLOB) AS X'
)
Thanks for all the help.
Viewing 10 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply