How to get info from an xml stored in a table column into different column

  • '<Root>

    <LineItem>

    <LineItemReference>30048</LineItemReference>

    </LineItem>

    <LineItem>

    <LineItemReference>30049</LineItemReference>

    </LineItem>

    </Root>'

    Have this xml stored in a column of a table and I need to take this data and store it in a different table it must be like this

    LineItemReference

    30048

    30049

    I need to do a bulk insert and don’t want to use a loop plz help

  • If I understand your question correctly, you are looking for something like:

    declare @xml xml

    set @xml = N'

    <Root>

    <LineItem>

    <LineItemReference>30048</LineItemReference>

    </LineItem>

    <LineItem>

    <LineItemReference>30049</LineItemReference>

    </LineItem>

    </Root>'

    select LineItems.LineItem.value(

    '.'

    ,'int'

    ) as LineItemValue

    from @xml.nodes(

    '//LineItem/LineItemReference'

    ) LineItems (LineItem)

    Some more information on XQuery and XML-DML -

    http://www.15seconds.com/Issue/050803.htm

  • hello

    what i need to do is select this xml from the column in a table where it is stored and insert the values form that xml into another table

  • sorry about the post before this is what i need to do plz help

    i have a table that have info stored like this :

    ID lineXML

    3 <Root><LineItem><LineItemReference>30048</LineItemReference></LineItem><LineItem><LineItemReference>30049</LineItemReference></LineItem></Root>

    4 <Root><LineItem><LineItemReference>30048</LineItemReference></LineItem><LineItem><LineItemReference>30049</LineItemReference></LineItem></Root>

    i need to take this info and store it in a different table and it must be displayed like this:

    ID line

    3 30048

    3 30049

    4 30048

    4 30049

  • are your looking for something like -

    declare @XmlTest table

    ( ID int identity(1,1), LineXml xml)

    insert into @XmlTest (LineXml)

    values

    ('

    <Root>

    <LineItem>

    <LineItemReference>30044</LineItemReference>

    </LineItem>

    <LineItem>

    <LineItemReference>30045</LineItemReference>

    </LineItem>

    </Root>'),

    ('

    <Root>

    <LineItem>

    <LineItemReference>30046</LineItemReference>

    </LineItem>

    <LineItem>

    <LineItemReference>30047</LineItemReference>

    </LineItem>

    </Root>'),

    ('

    <Root>

    <LineItem>

    <LineItemReference>30048</LineItemReference>

    </LineItem>

    <LineItem>

    <LineItemReference>30049</LineItemReference>

    </LineItem>

    </Root>')

    select ID,LineXml.value('(/Root/LineItem)[1]', 'int') as Line

    from @XmlTest

    union

    select ID,LineXml.value('(/Root/LineItem)[2]', 'int') as Line

    from @XmlTest

Viewing 5 posts - 1 through 4 (of 4 total)

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