[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Help needed using OpenXML

Posted on 2011-03-03
5
Medium Priority
?
505 Views
Last Modified: 2012-05-11
Hi.

I have a sql Server 2005 database. My web app has captured a series of answers from the user in xml format. I am passing this xml to a stored procedure and wish to extract the data into tables, but I'm struggling a bit with OpenXML to get the nested data out. I know how to pull out individual tag values, but I need to insert the data sets into these tables:
CandidateRole
CandidateSubmission

I have included a schema and the xml data file.

I would appreciate some hlep on this, i don't mind re-structuring the xml file if need be.

Many thanks.


cbp-schema.gif
cbp.xml
0
Comment
Question by:jonatec
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 40

Accepted Solution

by:
lcohan earned 1500 total points
ID: 35028365
Here's and example of how you can do that by using a SQL stored procedure:


CREATE PROCEDURE dbo.my_XML_update
      @XMLData NTEXT,
      @myId SMALLINT
AS
SET NOCOUNT ON

-- Retrieve XML data
DECLARE @hocDoc INT

DECLARE @Comments NVARCHAR(2048)

EXEC SP_XML_PREPAREDOCUMENT @hocDoc OUTPUT, @XMLData

-- OPEN XML
-- Param1 is Pointer to DOC
-- Param2 is XML doc hiearchy
-- Param3 is Use Element Structure rather than Attributes


SELECT
      ContactId,
      ContactName
INTO
      #tmpContact
FROM
      OPENXML(@hocDoc, '/root/Contact', 2)
WITH (ContactId  INTEGER, ContactName NVARCHAR(50))

--this is how you populate the contact xml
SELECT Contact.ContactId,
            ISNULL(Contact.ContactName, '') AS ContactName
FROM #tmpContact Contact, Contact C
WHERE      Contact.ContactId = C.Id
ORDER BY      Contact.ContactName
FOR XML AUTO, ELEMENTS

-- Then you can do an INSERT
INSERT INTO Contact (Id, ContactName)
      SELECT ContactId,ContactName
      FROM #tmpContact

--or an UPDATE
UPDATE Contact SET ContactName = t.ContactName
FROM #tmpContact t
                  WHERE      contact.Id = #tmpContact.ContactId
            
-- Drop the temp Table
DROP TABLE #tmpContact

-- and MUST release XML doc to avoid memeory issues
EXEC SP_XML_REMOVEDOCUMENT @hocDoc
GO
0
 

Author Comment

by:jonatec
ID: 35028456
Hi.

Thanks for that.

I can extract a simple data set, and presumably this is the case with your Contact data. However what if, as in the case of my example xml, the datasets are nested within the xml? Is it only possibel to extract at the lowest atomic level ?
0
 
LVL 40

Expert Comment

by:lcohan
ID: 35028675
No you should be able to extract complex XML and my example is indeed based on simple XML doc but yours is not complicated and is well formated xml doc - just make sure to pass right params - sorry I don't have that much time right now to write it for you...:

http://msdn.microsoft.com/en-us/library/ms186918.aspx
0
 

Author Closing Comment

by:jonatec
ID: 35028731
Ok thanks for the example !
0
 
LVL 40

Expert Comment

by:lcohan
ID: 35028750
your select should be something like:

SELECT *
FROM   OPENXML (@idoc, '/ROOT/domain/role/que',2)
         WITH (AccessID     varchar(10) '../@AccessID',
               CandidateID  varchar(10) '../@CandidateID',
               domid            varchar(10)    '../@domid',
               que_no      int         '@que_no',
               ans         int         '@ans')
0

Featured Post

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question