I want some Moore

Blog about stuff and things and stuff...
mostly about SQL server and .Net
posts - 156, comments - 1396, trackbacks - 33

My Links

SQLTeam.com Links

News

Hi! My name is 
Mladen Prajdić  I'm from Slovenia and I'm currently working as a .Net (C#) and SQL Server developer. I'm also a MCP and MCTS for SQL Server. 
Welcome to my blog.

Search this Blog
 

My Blog Feed via Email


Get your Google PageRank
Users Online: who's online

Article Categories

Archives

Post Categories

Cool software

Other Blogs

Other stuff

SQL stuff

SQL Server 2005: Easily importing XML Files

 

In SQL Server 2005 importing XML files became very easy.

OPENROWSET now supports the BULK keyword which lets us import XML files with ease.

A little example:

 

CREATE TABLE XmlImportTest
(
    xmlFileName VARCHAR(300),
    xml_data xml
)
GO

DECLARE @xmlFileName VARCHAR(300)
SELECT  @xmlFileName = 'c:\TestXml.xml'
-- dynamic sql is just so we can use @xmlFileName variable in OPENROWSET
EXEC('
INSERT INTO XmlImportTest(xmlFileName, xml_data)

SELECT ''' + @xmlFileName + ''', xmlData 
FROM
(
    SELECT  * 
    FROM    OPENROWSET (BULK ''' + @xmlFileName + ''' , SINGLE_BLOB) AS XMLDATA
) AS FileImport (XMLDATA)
')
GO
SELECT * FROM XmlImportTest

DROP TABLE XmlImportTest

 

SINGLE_BLOB is recommended when importing XML files because only it supports all Windows encoding conversions.

kick it on DotNetKicks.com

Print | posted on Monday, June 18, 2007 8:47 AM

Feedback

# re: SQL Server 2005: Easily importing XML Files

One question dealing with PDF files. I would like to accomplish the same results as you have illustrated here; howerver, on my website I wish to display the imported PDF file as a link.
12/13/2007 8:24 PM | Samson

# re: SQL Server 2005: Easily importing XML Files

you can, you just have to save it to varbinary(max) type column
1/7/2008 3:33 PM | Mladen

# re: SQL Server 2005: Easily importing XML Files

hi, i am facing a problem passing a parameter to a stored procedure in sql 2005. I'm new to it and all the xml stuffs..
Basically i want to pass in a xml file into 3 separate tables depending on the fields i require. currently i did 3 stored procedures to load the xml file and passing the data into these 3 tables. I am able to do it but I would like to pass in a parameter which is the xml file path.

Anyone can help me? Thanks in advance.

This is my store procedure:

ALTER PROC [dbo].[insertAccount]
as
DECLARE @hDoc int
DECLARE @xml xml

set @xml=(select convert(xml,BulkColumn, 2)
From openrowset(Bulk'C:\TestXml.xml', single_blob) [rowsetresults])

exec sp_xml_preparedocument @hDoc OUTPUT, @xml
INSERT INTO CB_Account
SELECT *
FROM OPENXML(@hDoc, 'REQUEST/MESSAGE/ACCOUNT',2)
WITH (RUN_NO BIGINT '../HEADER/RUN_NO',
ACYC BIGINT 'ACYC',
.....
ACON INTEGER 'ACON')
exec sp_xml_removedocument @hDoc
7/2/2008 9:38 AM | Desmond

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 3 and 6 and type the answer here:

Powered by: