0

我想选择除 InEx 值为“已排除”的元素以外的元素。

这是我的 XML 文档:

<MachineStatus>
<ShiftStop  MachineName="01" InEx="Excluded" />
<ShiftStop  MachineName="01" InEx="Included"  />
<ShiftStop  MachineName="01" InEx="Included"  />
  </MachineStatus>

我需要得到

<ShiftStop  MachineName="01" InEx="Included"  />
<ShiftStop  MachineName="01" InEx="Included"  />

我试过这个查询,但它不起作用。

SELECT [Entity].query('/MachineStatus/ShiftStop') FROM [FES].[SMD].[Machine] WHERE 
[Entity].value('(/MachineStatus/ShiftStop/@InEx)[1]','varchar(10)') != 'Excluded'
4

1 回答 1

0

当从 XML 中提取值时,它会“撕掉”整个标签,它会提取元素。如果要提取值,然后取回 XML 数据,则必须重新构建 XML:

CREATE TABLE dbo.YourTable (ID int IDENTITY,
                            YourXML xml);
INSERT INTO dbo.YourTable (YourXML)
VALUES('<MachineStatus>
<ShiftStop  MachineName="01" InEx="Excluded" />
<ShiftStop  MachineName="01" InEx="Included"  />
<ShiftStop  MachineName="01" InEx="Included"  />
</MachineStatus>');

SELECT YT.ID,
       (SELECT MS.SS.value('@MachineName','char(2)') AS [ShiftStop/@MachineName],
               MS.SS.value('@InEx','varchar(8)') AS [ShiftStop/@InEx]
        FROM YT.YourXML.nodes('MachineStatus/ShiftStop')MS(SS)
        WHERE MS.SS.value('@InEx','varchar(8)') != 'Excluded'
        FOR XML PATH(''),TYPE)
FROM dbo.YourTable YT;

GO

DROP TABLE.dbo.YourTable;
于 2020-09-17T09:07:26.233 回答