在sql server中向xml root添加属性

时间:2016-01-16 14:34:23

标签: sql-server xml

我正在尝试从sql server创建xml,我几乎完成了我的查询。但是,我有一个问题,我无法弄明白。如何向root“ITEM”添加属性,而不是“ITEMS”?我的sql查询在下面......

SELECT  
            CARD_TYPE
            ,CODE
            ,NAME
            ,UNITSET_CODE
            ,AUXIL_CODE
            ,AUXIL_CODE2
            ,AUXIL_CODE3
            ,AUXIL_CODE4
            ,AUXIL_CODE5
            ,CYPHCODE
       FROM P_ITEMS
      WHERE HOSTGROUPID='7155d850-5882-43a0-b722-f6586bfffa61'
        AND TRANCODE ='ITEMS'
        AND PROCESSED ='NEW'
        FOR XML PATH('ITEM'),ROOT('ITEMS')

1 个答案:

答案 0 :(得分:2)

尝试这样

SELECT  
        'theAttributValue' AS [@MyAttribut]
        ,CARD_TYPE
        ,CODE
        ,NAME
        ,UNITSET_CODE
        ,AUXIL_CODE
        ,AUXIL_CODE2
        ,AUXIL_CODE3
        ,AUXIL_CODE4
        ,AUXIL_CODE5
        ,CYPHCODE
   FROM P_ITEMS
  WHERE HOSTGROUPID='7155d850-5882-43a0-b722-f6586bfffa61'
    AND TRANCODE ='ITEMS'
    AND PROCESSED ='NEW'
    FOR XML PATH('ITEM'),ROOT('ITEMS')