用脚本导入到sql的xml文件数据

时间:2013-09-23 13:24:49

标签: sql sql-server-2005

我有这种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的新手。

提前致谢。

1 个答案:

答案 0 :(得分: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)

这为您提供了类似的输出:

enter image description here

....依此类推 - 列出所有变量及其名称和价值。