Home Forums SQL Server 2008 T-SQL (SS2K8) how to transform field name to attrribute value in for xml query RE: how to transform field name to attrribute value in for xml query

  • Yes it is, but in doing so, your field tag now contains integer and character data, so you will have troubles shredding it later.

    Select

    ca.doc

    From Main

    Left Join Docs on Main.Id = Docs.Id

    Left Join Tags on Main.Id = Tags.Id

    CROSS APPLY(

    SELECT [@name],

    int_data AS [text()],

    char_data AS [text()]

    FROM (

    VALUES

    ('Main', Main.Id, NULL),

    ('Tags', NULL, Tags.Tag),

    ('Docs', NULL, Docs.Doc)

    ) v([@name], int_data, char_data)

    FOR XML PATH('field'), TYPE

    ) ca(doc)

    FOR XML PATH('add'),TYPE

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA