SQL Server 2005 OpenXML - How do you load XML data containing nested elements/tags?

Hi,

Amazingly, I have been unable to easily find an answer to this simple question.

The problem is that there are plenty of blogs/articles on using OpenXml, but the examples use the lamest, most simplistic, single-level XML documents to load.

Ok, for example, I have table Contacts with the following colums:

ContactName
PhoneType
PhoneNum

The XML parameter data looks like:

<XMLDoc>
     <Contacts>
          <Name>John Doe</Name>
          <Phone>
               <Type>Cell</Type>
               <Number>555-555-5555</Number>
          </Phone>
     </Contacts>
</XML Doc>

So, in the SPROC, to load the input XML, the SQL would resemble:
...
INSERT Contacts(
     ContactName,
     PhoneType,
     PhoneNum
)
SELECT Name,
     Type
     Number
FROM OPENXML (@XML, '/Contacts',2)
WITH ( Name nvarchar (100),
           ?
           ?
)

My question is, how do you specify the data in nested "Phone" element?

Any and all help greatly appreciated.
Tony
ibcmisAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nativCommented:
SELECT Name, PhoneType
 FROM OPENXML (@idoc, '/XMLDoc/Contacts',2)
WITH ( Name nvarchar (100),
 PhoneType varchar(10) 'Phone/Type',
 PhoneNum varchar(10) 'Phone/Num',
)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ibcmisAuthor Commented:
Hi nativ,

Thank you for your response.

Your code looks like it should work, and as soon as I try it, I will "accept" it as the solution.

Thanks again.

Anthony PerkinsCommented:
Just some minor typos in your Xml and the solution provided (please no points):
<XMLDoc>
     <Contacts>
          <Name>John Doe</Name>
          <Phone>
               <Type>Cell</Type>
               <Number>555-555-5555</Number>
          </Phone>
     </Contacts>
</XMLDoc>

SELECT  Name,
        PhoneType,
        PhoneNum
FROM    OPENXML (@idoc, '/XMLDoc/Contacts',2)
WITH ( Name nvarchar (100),
 PhoneType varchar(10) 'Phone/Type',
 PhoneNum varchar(10) 'Phone/Number')

Open in new window

10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

Anthony PerkinsCommented:
Having said that, you may find the given an Xml structure like this:

<XMLDoc>
     <Contacts>
          <Name>John Doe</Name>
          <Phone>
               <Type>Cell</Type>
               <Number>555-555-5555</Number>
          </Phone>
          <Phone>
               <Type>Business</Type>
               <Number>666-666-6666</Number>
          </Phone>
     </Contacts>
</XMLDoc>

The following OPENXML query is more generic and will give you all the phones, not just the first one:
SELECT  Name,
        PhoneType,
        PhoneNum
FROM    OPENXML (@idoc, '/XMLDoc/Contacts/Phone', 2) WITH (
		Name nvarchar (100) '../Name',
		PhoneType varchar(10) 'Type',
		PhoneNum varchar(10) 'Number')

Open in new window

ibcmisAuthor Commented:
Nativ,

Thank you very much. The snippet you provided was what I needed.

Tony
ibcmisAuthor Commented:
Short, sucinct, and correct. Couldn't ask for more.
ibcmisAuthor Commented:
acperkins,

Thank you for your additional example...I believe you read my mind.

Although my current project doesn't require it, I was also thinking of just the scenario you astutely addressed in your example. I just didn't want to muddy the waters by bringing it up.

Thanks again for your contribution.

Tony
Anthony PerkinsCommented:
Thank you and I appreciate the comments and the explanation.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.