How do I ...?

  • Hi all,

    I know nothing about processing an XML file in SQLServer.

    What I do know is: I have an xml file on the network and I need to retrieve some data from it.

    The file structure is:

    <?xml version="1.0" encoding="utf-8" ?>

    <Cfo>

    <Env>T3</Env>

    <Out>D:\Out</Out>

    <Comp>

    <Comp>

    <Comp>

    <Comp>

    <Rev>

    <Dep>12345</Dep>

    <DepDate>Wed, 08 May 2013 14:01:29 GMT</DepDate>

    </Rev>

    </Cfo>

    Looking for your help and guidance on how to approach this. Do I need to load xml file into a table first?

    How do I retrieve values from <Env> and <Rev> nodes???

    Thanks,

  • You can store it in a table or a variable and work with it ok. It just depends what you need to do with the XML.. Once you have shredded the XML, do you need it anymore? if not then storing it in a variable will be just fine..

    Here is a way to get the XML from file into a variable to work with it:

    DECLARE @xml XML

    SELECT @xml = (SELECT * FROM OPENROWSET(BULK 'c:\Temp\SomeXmlFile.Xml', SINGLE_BLOB) x)

    The filepath does need to be relative to the SQL server and the account that the SQL server service runs under will need to have permissions to access the file. If you are running SQL server under the localsystem account, then I don't believe it will be able to access the file over the network.

    Once you have it in a variable, then you can use the XML methods to begin to shred it. The most simplest one is value(). Another good one which helps with shredding XML to a table is nodes(). There are loads of blog posts on shredding XML to a table using nodes(). I've done a couple myself 🙂

    Here is a simple example using the value() method:

    SELECT @xml.value('(/Cfo/Env/text())[1]', 'varchar(20)')

  • Thank you very much, it worked for me.

    What is your blog URL?

  • rightontarget (5/10/2013)


    Thank you very much, it worked for me.

    What is your blog URL?

    Thanks. glad it helped

    my blog is syndicated right here at SSC http://www.sqlservercentral.com/blogs/rocks/

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply