Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How to read this xml in sql 2005 Expand / Collapse
Author
Message
Posted Friday, May 11, 2012 1:55 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, December 3, 2012 10:41 PM
Points: 14, Visits: 24
Hi,
how to read this XML in sql 2005 and store the data into temptable.
Please help me.

'<DocumentElement>
<QuestionList>
<QuestionId>Q1000</QuestionId>
<Title>Apple /Title>
<Sequence>3</Sequence>
<MergeSequence>1</MergeSequence>
</QuestionList>
<QuestionList>
<QuestionId>Q1028</QuestionId>
<Title>Banana/Title>
<Sequence>3</Sequence>
<MergeSequence>2</MergeSequence>
</QuestionList>
<QuestionList>
<QuestionId>Q1029</QuestionId>
<Title>orange/Title>
<Sequence>3</Sequence>
<MergeSequence>3</MergeSequence>
</QuestionList>
</DocumentElement>'

Output

QuestioID Title Seq  MerSeq
Q1000 Apple 3 1
Q1028 Banan 3 2
Q1029 Orange 3 3




Thanks
Narayanan
Post #1298393
Posted Sunday, May 13, 2012 10:42 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 6:03 PM
Points: 3,609, Visits: 5,221
Note that all of your title tags were incorrectly constructed. After fixing that, I came up with the following:

DECLARE @XML XML

SET @XML = CAST('<DocumentElement>
<QuestionList>
<QuestionId>Q1000</QuestionId>
<Title>Apple</Title>
<Sequence>3</Sequence>
<MergeSequence>1</MergeSequence>
</QuestionList>
<QuestionList>
<QuestionId>Q1028</QuestionId>
<Title>Banana</Title>
<Sequence>3</Sequence>
<MergeSequence>2</MergeSequence>
</QuestionList>
<QuestionList>
<QuestionId>Q1029</QuestionId>
<Title>orange</Title>
<Sequence>3</Sequence>
<MergeSequence>3</MergeSequence>
</QuestionList>
</DocumentElement>' AS XML)

SELECT x.xml.value('QuestionId[1]','VARCHAR(5)') As QuestionID
,x.xml.value('Title[1]','VARCHAR(10)') AS Title
,x.xml.value('Sequence[1]','INT') AS Sequence
,x.xml.value('MergeSequence[1]','INT') AS MergeSequence
FROM @xml.nodes('//DocumentElement/QuestionList') x(xml)





My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1299343
Posted Tuesday, May 22, 2012 3:48 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, December 3, 2012 10:41 PM
Points: 14, Visits: 24
Thanks dwain
Post #1304004
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse