可以帮我从XML中获取一些数据吗?

时间:2019-05-23 10:58:10

标签: sql sql-server xml

从多个XML获取SQL数据

我已经尝试将代码放入XML变量中,并选择OrderNumber,ProductionLine和ItemId,但是查询时遇到了一些麻烦。

    DECLARE @DXML XML = '<ComDecom OrderNumber="101983026" 
    ProductionLine="14" BatchNumber="02-00" ItemObjectTypeId="1" 
    ItemFlag="20" EventGuid="989bfdb4-9dd8-40be-9872-1e0bae7cc4d6" 
    LastMessage="false" HostName="PMIPTLISWCT0014+1">
      <Item ItemId="LESTCNNGxDDCPq1bSF1S119052306" TimeStamp="2019-05-23 
     07:56:07.475 +01:00" SeqNumber="175660" />
      <Item ItemId="LESTCNNGxDDCPq1bSF1S119052306" TimeStamp="2019-05-23 
     07:56:07.519 +01:00" SeqNumber="175661" />
      <Item ItemId="LESTCNoTmCiiVu1bSF1S119052306" TimeStamp="2019-05-23 
     07:56:08.487 +01:00" SeqNumber="175662" /> 
                </ComDecom>'

     SELECT ComDeCom.value('@OrderNumber', 'int') AS OrderNumber
        ,ComDecom.value('@ProductionLine', 'int') AS ProductionLine
        ,ItemTbl.value('@ItemId', 'varchar') AS Item
     FROM @dxml.nodes('/ComDecom/') AS ComDecomTbl(ComDecom)
     CROSS APPLY ComDecom.Item.nodes('Site') AS ItemTbl(Item)

1 个答案:

答案 0 :(得分:0)

我认为您正在寻找这个

DECLARE @DXML XML= 
'<ComDecom OrderNumber="101983026" ProductionLine="14" BatchNumber="02-00" ItemObjectTypeId="1" ItemFlag="20" EventGuid="989bfdb4-9dd8-40be-9872-1e0bae7cc4d6" LastMessage="false" HostName="PMIPTLISWCT0014+1">
  <Item ItemId="LESTCNNGxDDCPq1bSF1S119052306" TimeStamp="2019-05-23       07:56:07.475 +01:00" SeqNumber="175660" />
  <Item ItemId="LESTCNNGxDDCPq1bSF1S119052306" TimeStamp="2019-05-23       07:56:07.519 +01:00" SeqNumber="175661" />
  <Item ItemId="LESTCNoTmCiiVu1bSF1S119052306" TimeStamp="2019-05-23       07:56:08.487 +01:00" SeqNumber="175662" />
</ComDecom>';


SELECT 
T.N.value('@OrderNumber', 'int') AS OrderNumber,
T.N.value('@ProductionLine', 'int') AS ProductionLine,
T2.N2.value('@ItemId', 'varchar(MAX)') AS Item
FROM @dxml.nodes('/ComDecom') AS T(N)
CROSS APPLY @dxml.nodes('/ComDecom/Item') AS T2(N2)

输出为-

OrderNumber ProductionLine  Item
101983026   14              LESTCNNGxDDCPq1bSF1S119052306
101983026   14              LESTCNNGxDDCPq1bSF1S119052306
101983026   14              LESTCNoTmCiiVu1bSF1S119052306