How to add attributes to xml nodes in sql server 2005

前端 未结 3 1616
北恋
北恋 2021-01-13 14:41

If i wanted to add an attribute to the root element record, can i do this from the sql side?

SELECT top 1 \'text\' as nodeA
                from test as z
FO         


        
3条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-13 14:54

    Use the new FOR XML PATH syntax:

    SELECT TOP 1 
       'someValue' AS '@Attribute',
       'text' as 'z/NodeA'
    FROM dbo.Test
    WHERE....
    FOR XML PATH('YourElement'), ROOT('Root')
    

    This would give something like

    
       
          
             text
          
       
    
    

    Read more about it here:

    • Simple Example of Creating XML File Using T-SQL
    • Using XML Serialization with SQL's FOR XML PATH

提交回复
热议问题