XML 中的数据 Table 字符串到 SQL Table

Data Table in XML String to SQL Table

我希望有人能提供一个小例子,将 XML 字符串中的数据 table 发送到 SQL table。我一直在搜索此站点和互联网,但我发现的示例具有不同的 XML 布局。我没有足够的经验将这些例子应用到我的身上。这是 XML 的样子,我想要的数据在最后,在 "rs:data" 部分,注意:这可能是几 1000 行:

DECLARE @XML XML
SET @XML = '<xml xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema">
    <x:PivotCache>
        <x:CacheIndex>1</x:CacheIndex>
        <s:Schema id="RowsetSchema">
            <s:ElementType name="row" content="eltOnly">
                <s:attribute type="Col1"/>
                <s:attribute type="Col2"/>
                <s:attribute type="Col3"/>
                <s:attribute type="Col4"/>
                <s:attribute type="Col5"/>
                <s:attribute type="Col6"/>
                <s:attribute type="Col7"/>
                <s:attribute type="Col8"/>
                <s:attribute type="Col9"/>
                <s:extends type="rs:rowbase"/>
            </s:ElementType>
            <s:AttributeType name="Col1" rs:name=" Date">
                <s:datatype dt:type="dateTime"/>
            </s:AttributeType>
            <s:AttributeType name="Col2" rs:name=" PlantId">
                <s:datatype dt:maxLength="255"/>
            </s:AttributeType>
            <s:AttributeType name="Col3" rs:name=" Provider">
                <s:datatype dt:maxLength="255"/>
            </s:AttributeType>
            <s:AttributeType name="Col4" rs:name=" Pipeline">
                <s:datatype dt:maxLength="255"/>
            </s:AttributeType>
            <s:AttributeType name="Col5" rs:name=" Group">
                <s:datatype dt:maxLength="255"/>
            </s:AttributeType>
            <s:AttributeType name="Col6" rs:name=" Type">
                <s:datatype dt:maxLength="255"/>
            </s:AttributeType>
            <s:AttributeType name="Col7" rs:name=" Description">
                <s:datatype dt:maxLength="255"/>
            </s:AttributeType>
            <s:AttributeType name="Col8" rs:name=" DataType">
                <s:datatype dt:maxLength="255"/>
            </s:AttributeType>
            <s:AttributeType name="Col9" rs:name=" DataValue">
                <s:datatype dt:type="float"/>
            </s:AttributeType>
        </s:Schema>
        <rs:data>
            <z:row Col1="2015-01-01T00:00:00" Col2="CH1" Col3="ENOGEX" Col4="ENOGEX" Col5="Fixed" Col6="Transport" Col7="Monthly Reservation" Col8="Cost" Col9="9483.1183870967743"/>
            <z:row Col1="2015-01-02T00:00:00" Col2="CH1" Col3="ENOGEX" Col4="ENOGEX" Col5="Fixed" Col6="Transport" Col7="Monthly Reservation" Col8="Cost" Col9="9483.1183870967743"/>
            <z:row Col1="2015-01-03T00:00:00" Col2="CH1" Col3="ENOGEX" Col4="ENOGEX" Col5="Fixed" Col6="Transport" Col7="Monthly Reservation" Col8="Cost" Col9="9483.1183870967743"/>
            <z:row Col1="2015-01-04T00:00:00" Col2="CH1" Col3="ENOGEX" Col4="ENOGEX" Col5="Fixed" Col6="Transport" Col7="Monthly Reservation" Col8="Cost" Col9="9483.1183870967743"/>
            <z:row Col1="2015-01-05T00:00:00" Col2="CH1" Col3="ENOGEX" Col4="ENOGEX" Col5="Fixed" Col6="Transport" Col7="Monthly Reservation" Col8="Cost" Col9="9483.1183870967743"/>
            <z:row Col1="2015-01-06T00:00:00" Col2="CH1" Col3="ENOGEX" Col4="ENOGEX" Col5="Fixed" Col6="Transport" Col7="Monthly Reservation" Col8="Cost" Col9="9483.1183870967743"/>
        </rs:data>
    </x:PivotCache>
</xml>'

我想要的所有数据都在 "rs:data" 部分。无论多行,它都是 9 列。 XML 中的每一行都以 "z:row" 开头。这是我试过的,这个查询执行没有错误,显示我的列 headers,但没有数据:

DECLARE @TempTbl TABLE ([Date] datetime, PlantId varchar(50), Provider varchar(50), Pipeline varchar(50), [Group] varchar(50), 
    [Type] varchar(50), [Description] varchar(50), DataType varchar(50), DataValue float) 

INSERT INTO @TempTbl 
SELECT  Tbl.Col.value('@Col1', 'datetime'), 
        Tbl.Col.value('@Col2', 'VARCHAR(50)'), 
        Tbl.Col.value('@Col3', 'VARCHAR(50)'),
        Tbl.Col.value('@Col4', 'VARCHAR(50)'), 
        Tbl.Col.value('@Col5', 'VARCHAR(50)'), 
        Tbl.Col.value('@Col6', 'VARCHAR(50)'), 
        Tbl.Col.value('@Col7', 'VARCHAR(50)'), 
        Tbl.Col.value('@Col8', 'VARCHAR(50)'), 
        Tbl.Col.value('@Col9', 'float')
FROM @xml.nodes('//row') Tbl(Col) 

--See the table 
SELECT * FROM @TempTbl

您需要尊重并在您的文档中包含XML命名空间

试试这个:

-- define the relevant XML namespaces you need
;WITH XMLNAMESPACES('urn:schemas-microsoft-com:office:excel' AS x, 
                   'urn:schemas-microsoft-com:rowset' AS rs,
                   '#RowsetSchema' AS z)
INSERT INTO @TempTbl 
    SELECT  
        Tbl.Col.value('@Col1', 'datetime'), 
        Tbl.Col.value('@Col2', 'VARCHAR(50)'), 
        Tbl.Col.value('@Col3', 'VARCHAR(50)'),
        Tbl.Col.value('@Col4', 'VARCHAR(50)'), 
        Tbl.Col.value('@Col5', 'VARCHAR(50)'), 
        Tbl.Col.value('@Col6', 'VARCHAR(50)'), 
        Tbl.Col.value('@Col7', 'VARCHAR(50)'), 
        Tbl.Col.value('@Col8', 'VARCHAR(50)'), 
        Tbl.Col.value('@Col9', 'float')
    FROM @xml.nodes('/xml/x:PivotCache/rs:data/z:row') Tbl(Col) 

--See the table 
SELECT * FROM @TempTbl

这应该会为您提供 table 变量中的属性值