How to read this xml in sql 2005

  • 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&gt

    <Sequence>3</Sequence>

    <MergeSequence>1</MergeSequence>

    </QuestionList>

    <QuestionList>

    <QuestionId>Q1028</QuestionId>

    <Title>Banana/Title&gt

    <Sequence>3</Sequence>

    <MergeSequence>2</MergeSequence>

    </QuestionList>

    <QuestionList>

    <QuestionId>Q1029</QuestionId>

    <Title>orange/Title&gt

    <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

  • 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![/I]

    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?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • Thanks dwain

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply