您好我试图从我的sql查询生成xml。但我在我的xml中添加额外的元素时遇到了问题。或者甚至可以这样做。我的SQL查询是这样的。
SELECT
m.DeviceId as IndividualItemNo,
Name = 'B',
w.BTime as Value,
CAST(CONVERT(VARCHAR(8), w.EndTS, 112) AS INT) AS [Date],
CONVERT(VARCHAR(5), w.EndTS,108) + '+0000' AS [Time],
g.GroupName
FROM Machine m
INNER JOIN [Group] g ON m.GroupId = g.GroupId
INNER JOIN [EDWCompany] e ON g.CompanyKey = e.CompanyKey
LEFT JOIN WorkingPass w ON m.MachineId = w.MachineId and w.WorkingPassId = (SELECT MAX(w.WorkingPassId) FROM WorkingPass w WHERE MachineId = m.MachineId )
WHERE g.CompanyKey = 105
GROUP BY m.DeviceId, w.EndTS , w.BTime , g.GroupName, e.CompanyNo, m.MachineId
ORDER BY m.DeviceId
FOR XML PATH ('IndividualItem'), ROOT ('IndividualItem_Report'), ELEMENTS
我的XML结果:
<IndividualItem_Report>
<IndividualItem>
<IndividualItemNo>027FDF3014953</IndividualItemNo>
<Name>B</Name>
<Value>3535</Value>
<Date>20130221</Date>
<Time>10:32+0000</Time>
<GroupName>TARKETT AB</GroupName>
</IndividualItem>
</IndividualItem_Report>
但我想添加额外的元素。所以这就是我想要的。
<IndividualItem_Report>
<IndividualItem>
<IndividualItemNo>027FDF3014953</IndividualItemNo>
<HourMeterList>
<HourMeter>
<Name>B</Name>
<Value>3535</Value>
<Date>20130221</Date>
<Time>10:32+0000</Time>
<GroupName>TARKETT AB</GroupName>
</HourMeter>
</HourMeterList>
</IndividualItem>
</IndividualItem_Report>
答案 0 :(得分:0)
我假设HourMeterList是一个多值
从工作代码
这只是选择列表的一部分
,(select top 1 'MV'
from docMVenum1 with (nolock)
where docMVenum1.sID = docSVsys.sID and docMVenum1.enumID = '179')
as 'RFPResponse/@SVMV'
,(select top 1 'RFP Response#'
from docMVenum1 with (nolock)
where docMVenum1.sID = docSVsys.sID and docMVenum1.enumID = '179')
as 'RFPResponse/@displayName'
, (select docEnum1.value
from docMVenum1 with (nolock)
join docEnum1 with (nolock)
on docEnum1.enumID = docMVenum1.enumID and docEnum1.valueID = docMVenum1.valueID
where docMVenum1.sID = docSVsys.sID and docMVenum1.enumID = '179'
order by docEnum1.value
FOR XML PATH (''), type
) as [RFPResponse]
示例输出
<RFPResponse SVMV="MV" displayName="RFP Response#">
<value>RFPa</value>
<value>RFPb</value>
</RFPResponse>