Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2005 > SQL Server 2005
|
SQL Server 2005 General discussion of SQL Server *2005* version only.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 23rd, 2006, 07:58 AM
Authorized User
 
Join Date: Sep 2004
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default Looping through XML in SQL Server 2005

Hi Guys,
The number of postings seem to be few, but if anyone is able to help I'd appreciate it.

Basically, I'm passing a string of XML as an nvarchar to a stored procedure. I then convert the string to XML using:

SET @XML = CONVERT(xml, RTrim(@ApplicantDetailsXML))

My Applicant Details XML may look like this:

<?xml version="1.0"?>
<Applicant>
    <Title>Mr</Title>
    <Forename>Tommy</Forename>
    <Surname>Test</Surname>
    <DateOfBirth>01/01/1975</DateOfBirth>
    <Gender>M</Gender>
    <Addresses>
        <Address>
            <Flat>
            </Flat>
            <HouseName>
            </HouseName>
            <HouseNumber>1</HouseNumber>
            <Street>Test Street</Street>
            <District>Test District</District>
            <Town>Test Town</Town>
            <County>Test County</County>
            <Postcode>TEST1</Postcode>
            <CountryCode>GB</CountryCode>
            <AtAddressFrom>01/01/2002</AtAddressFrom>
            <AtAddressTo>22/02/2006</AtAddressTo>
            <AddressType>U</AddressType>
        </Address>
        <Address>
            <Flat>Flat 2</Flat>
            <HouseName>Big House Of Flats</HouseName>
            <HouseNumber>2</HouseNumber>
            <Street>Test Street</Street>
            <District>Test District</District>
            <Town>Test Town</Town>
            <County>Test County</County>
            <Postcode>TEST2</Postcode>
            <CountryCode>GB</CountryCode>
            <AtAddressFrom>01/01/2000</AtAddressFrom>
            <AtAddressTo>31/12/2001</AtAddressTo>
            <AddressType>U</AddressType>
        </Address>
    </Addresses>
</Applicant>

I've setup a "loop"(it's not really) to go through the address nodes so that I can save them one at a time.

I can only seem to find an example like this:

EXEC sp_xml_preparedocument @docHandle OUTPUT, @XML
INSERT dbo.Address (HouseName)
    SELECT CONVERT(nvarchar(10), Text)
FROM OPENXML(@docHandle, '//Applicant/Addresses/Address/HouseName/text()', 1)
EXEC sp_xml_removedocument @docHandle

I can only get it to pick off one of the elements at a time. Are there any ways I can pick off all the elements for my insert at the same time?

I'd appreciate any tips on how best to handle this.
Thanks,
Francis

 
Old February 23rd, 2006, 11:47 AM
Authorized User
 
Join Date: Sep 2004
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This does the job but I'm sure there must be a better way in 2005! If anyone can find out I'd be really interested to know.

DECLARE @dochandle integer
EXEC sp_xml_preparedocument @docHandle OUTPUT, @XML

SELECT @Flat = Flat, @HouseName = HouseName, @HouseNumber = HouseNumber,
        @Street = Street, @District = District, @Town = Town, @County = County,
        @Postcode = Postcode, @CountryCode = CountryCode, @AtAddressFrom = AtAddressFrom,
        @AtAddressTo = AtAddressTo, @AddressType = AddressType
FROM OPENXML(@docHandle, '//Applicant/Addresses/Address', 2)
With (

        Flat nchar(20),
        HouseName nchar(32),
        HouseNumber nchar(32),
        Street nchar(40),
        District nchar(40),
        Town nchar(40),
        County nchar(40),
        Postcode nchar(10),
        CountryCode smallint,
        AtAddressFrom datetime,
        AtAddressTo datetime,
        AddressType smallint
    )

INSERT INTO dbo.Addresses(
            AddressType,
            Flat,
            HouseName,
            HouseNumber,
            Street,
            District,
            Town,
            County,
            Postcode,
            CountryCode,
            AtAddressFrom,
            AtAddressTo
     )
     VALUES(
            1,
            @Flat,
            @HouseName,
            @HouseNumber,
            @Street,
            @District,
            @Town,
            @County,
            @Postcode,
            @CountryCode,
            @AtAddressFrom,
            @AtAddressTo
    )
EXEC sp_xml_removedocument @docHandle








Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL Server 2005 XML: FOR XML PATH -> cdata? stoves SQL Server 2005 1 July 8th, 2008 02:40 AM
reg conn to sql server 2005 from vb.net 2005.. veda SQL Server 2005 2 July 1st, 2008 12:16 AM
Conflict in SQL Server 2000 and SQL Server 2005 ayan.mukherjee SQL Language 0 June 30th, 2008 03:34 AM
XML vs SQL Server 2005 yadavrahul2k5 XML 2 June 1st, 2008 02:59 AM
Transform SQL server 2005 XML with xslt bonekrusher XSLT 0 July 11th, 2007 07:45 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.