FOR XML

  • How do I add an attribute to the ROOT node in a FOR XML statement?

    FOR XML ROOT('root_element') ????

  • The description of the ROOT directive in BOL seems to imply that you can't add an attribute via that mechanism, but you could try (without using gibberish data obviously ) FOR XML PATH or FOR XML EXPLICIT (from what you've said, I'm guessing EXPLICIT is the route you need):

    set

    nocount on

    declare

    @t table (a int identity(1,1), b char(1))

    insert into @t (b) values('a')

    insert into @t (b) values('b')

    insert into @t (b) values('c')

    select

    a '@id', b from @t

    for xml path--, root('MyRoot')

    select 1 Tag, null Parent, 'rootAttribute' 'Id!1!id', null 'Letter!2!id' from @t

    where a = 1

    union all

    select 2 Tag, 1 Parent, null, b from @t

    for xml explicit

    set nocount off

    Jon

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

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