1

我有这种xml:

<?xml version="1.0"?>
-<recordedData>
    <machine>ZSK40-2</machine>
    <date>2013/09/21</date>
    <hour>05:32</hour>-<CollectedData>-<variable>
            <Name>PRODUCT</Name>
            <Value>FILLER 580</Value>
        </variable>-<variable>
            <Name>LOT_NUMBER</Name>
            <Value>CG 00063 0</Value>
        </variable>-<variable>
            <Name>SHIFT_SUPERVISOR</Name>
            <Value>  covaliu l</Value>
        </variable>-<variable>
            <Name>KGH_ALL_SET</Name>
            <Value>0</Value>
        </variable>-<variable>
            <Name>KGH_ALL_REAL</Name>
            <Value>0</Value>
        </variable>-<variable>
            <Name>KGH_F1_SET</Name>
            <Value>0</Value>
        </variable>-<variable>
            <Name>KGH_F1_REAL</Name>
            <Value>0</Value>
        </variable>-<variable>
            <Name>K_F1</Name>
            <Value>43</Value>
        </variable>-<variable>
            <Name>SCREW_RPM_SET</Name>
            <Value>550</Value>
        </variable>-<variable>
            <Name>SCREW_RPM_REAL</Name>
            <Value>550.085388183594</Value>
        </variable>-<variable>
            <Name>TORQUE</Name>
            <Value>1.21340000629425</Value>
        </variable>-<variable>
            <Name>CURRENT</Name>
            <Value>60.1959991455078</Value>
        </variable>-<variable>
            <Name>KW_KG</Name>
            <Value>0</Value>
        </variable>-<variable>
            <Name>KW</Name>
            <Value>-0.990000009536743</Value>
        </variable>-<variable>
            <Name>MELT_PRESSURE</Name>
            <Value>0</Value>
        </variable>-<variable>
            <Name>MELT_TEMPERATURE</Name>
            <Value>214</Value>
        </variable>-<variable>
            <Name>PV1</Name>
            <Value>216</Value>
        </variable>-<variable>
            <Name>SP1</Name>
            <Value>210</Value>
        </variable>-<variable>
            <Name>PV2</Name>
            <Value>239</Value>
        </variable>-<variable>
            <Name>SP2</Name>
            <Value>220</Value>
        </variable>-<variable>
            <Name>PV3</Name>
            <Value>220</Value>
        </variable>-<variable>
            <Name>SP3</Name>
            <Value>220</Value>
        </variable>-<variable>
            <Name>PV4</Name>
            <Value>220</Value>
        </variable>-<variable>
            <Name>SP4</Name>
            <Value>220</Value>
        </variable>-<variable>
            <Name>PV5</Name>
            <Value>209</Value>
        </variable>-<variable>
            <Name>SP5</Name>
            <Value>210</Value>
        </variable>-<variable>
            <Name>PV6</Name>
            <Value>210</Value>
        </variable>-<variable>
            <Name>SP6</Name>
            <Value>210</Value>
        </variable>-<variable>
            <Name>PV7</Name>
            <Value>210</Value>
        </variable>-<variable>
            <Name>SP7</Name>
            <Value>210</Value>
        </variable>-<variable>
            <Name>PV8</Name>
            <Value>210</Value>
        </variable>-<variable>
            <Name>SP8</Name>
            <Value>210</Value>
        </variable>-<variable>
            <Name>PV9</Name>
            <Value>210</Value>
        </variable>-<variable>
            <Name>SP9</Name>
            <Value>210</Value>
        </variable>-<variable>
            <Name>PV10</Name>
            <Value>210</Value>
        </variable>-<variable>
            <Name>SP10</Name>
            <Value>210</Value>
        </variable>-<variable>
            <Name>PV11</Name>
            <Value>220</Value>
        </variable>-<variable>
            <Name>SP11</Name>
            <Value>220</Value>
        </variable>
    </CollectedData>
</recordedData>

任何人都可以提供一个示例 sql 脚本来从中提取所有数据。

我真的很欣赏这一点,因为我是 xml 新手。

提前致谢。

4

1 回答 1

1

如果您的数据已经在表中,则可以使用以下内容:

DECLARE @Tmp TABLE (ID INT NOT NULL, XmlContent XML)

INSERT INTO @TMP VALUES(1, '......(your entire XML here).......)

SELECT
    ID,
    MACHINE = XmlContent.value('(/recordedData/machine)[1]', 'varchar(50)'),
    RecordingDate = XmlContent.value('(/recordedData/date)[1]', 'varchar(50)'),
    RecordingTime = XmlContent.value('(/recordedData/hour)[1]', 'varchar(50)'),
    VariableName = XVar.value('(Name)[1]', 'varchar(50)'),
    VariableValue = XVar.value('(Value)[1]', 'varchar(50)')
FROM
    @Tmp
CROSS APPLY 
    XmlContent.nodes('/recordedData/CollectedData/variable') AS XTbl(XVar)

这会为您提供类似以下的输出:

在此处输入图像描述

.... 等等 - 列出所有变量及其名称和值。

于 2013-09-23T13:46:57.360 回答