Solved

Help needed using OpenXML

Posted on 2011-03-03
5
499 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
  • 3
  • 2
5 Comments
 
LVL 39

Accepted Solution

by:
lcohan earned 500 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 39

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 39

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now