我有这样的查询:
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
必须像这样
感谢您的帮助..
答案 0 :(得分:0)
你需要check out the FOR XML PATH()
syntax - 它比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')