嗨,我试图从我的 t 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>