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

时间:2015-02-15 17:11:12

标签: sql-server xml xquery

我希望有人可以提供一个将XML String中的数据表发送到SQL表的小例子。我一直在搜索这个网站和互联网,但我发现的例子有不同的XML布局。我没有足够的经验将这些例子用于我的。以下是XML的样子,我想要的数据最后是“rs:data”部分,注意:这可能是几千行:

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”开头。这是我尝试过的,这个查询执行没有错误,显示我的列标题,但没有数据:

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

1 个答案:

答案 0 :(得分:0)

您需要尊重并在文档中包含 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

这应该将属性值赋予表变量