-1

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

1 回答 1

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>
于 2013-05-03T15:21:29.650 回答