1

我有一个这样的查询:

 SELECT  
    getdate()[createdon], 'www.google.com'[XMLNS],
    Co.Country, IC.InterConnectionName as point,
    SO.SOName[TSO], FD.GasDay, FD.CaptureDate,
    CD.mwhentry AS entrymwh, CD.mwhexit AS exitmwh, 
    DS.State, (CD.MWHENTRY - CD.MWHEXIT) AS NETMWH,
    (cd.mwhexit - cd.mwhexit) as NETMCH 
FROM 
    TABLENAME 

我希望将其转换为 xml

<gas-flow created-on="2012/06/02T10:30:01Z"
          xmlns="www.google.com">
   <country>Austria</country>
   <point>TSO4</point>
   <tso>BOG</tso>
   <gas-day>2012/06/02</gas-day>
   <capture-date>2012/06/02T10:30:00Z</capture-date>
   <state version="1">provisional</state>  
   <entry mwh="558234" mcm="52.80"/>
   <exit mwh="0" mcm="0"/>
   <net mwh="558234" mcm="52.80"/>
</gas-flow>

如果我这样做

FOR XML RAW ('gasflow'), ROOT ('FLOWDATAID'), ELEMENTS

我也得到标签之间的数据,但问题是

 entry tag, exit tag and net mwh 

必须是这样的

谢谢您的帮助..

4

1 回答 1

0

您需要检查FOR XML PATH()语法FOR XML RAW- 它比并允许您执行诸如将某些输出指定为属性(而不是元素)之类的事情要灵活得多。

所以尝试这样的事情:

;WITH XMLNAMESPACES (DEFAULT 'www.google.com')
SELECT  
    GETDATE() AS '@created-on', 
    CO.Country, 
    IC.InterConnectionName AS 'point',
    SO.SOName AS TSO, 
    FD.GasDay, 
    FD.CaptureDate,
    '1' AS 'State/@version',
    [DS.State], 
    CD.mwhentry AS 'entry/@mwh',
    CD.mcmEntry AS 'entry/@mcm',
    CD.mwhexit AS 'exit/@mwh',
    CD.mcmexit AS 'exit/@mcm',
    (CD.MWHENTRY - CD.MWHEXIT) AS 'net/@mwh',
    (CD.MWHexit - CD.MWHexit) AS 'net/@mcm'
FROM 
    dbo.YourTableNameHere
FOR XML PATH('gas-flow'), ROOT ('FLOWDATAID')
于 2012-11-26T06:26:31.393 回答