Home Forums SQL Server 2008 T-SQL (SS2K8) How do I use one column for node names and the others for elements in that node? RE: How do I use one column for node names and the others for elements in that node?

  • Here's a version of FLWOR which approximates what you want (again - with the name isssue handled)

    declare @x xml

    ;with a as (

    select 'a123' partID, 'blue' color,'large' size

    union all

    select 'a124' partID, 'green' color,'medium' size

    union all

    select 'a125' partID, 'red' color,'small' size)

    select @x=(select partid as "@partid",color as "color", size from a for XML path ('part'), root('PartList'), type)

    select cast(replace(replace(cast(

    @x.query

    ('

    for $a in PartList,

    $e in $a/part

    return <partlist>

    {concat("<",string($e/@partid),">")}

    {$e/*}

    {concat("</",string($e/@partid),">")}

    </partlist>

    ') as varchar(max)),'<','<'),'>','>') as XML)

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?