Solved

Help needed using OpenXML

Posted on 2011-03-03
5
495 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

706 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

16 Experts available now in Live!

Get 1:1 Help Now