无法使用insert(XML DML)

时间:2015-12-08 16:59:05

标签: sql sql-server tsql xml-dml

我正在尝试使用XML插入(XML DML)在XML中插入节点。

XML看起来像这样:

<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
    <Worksheet xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" ss:Name="1">
        <Table xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
            <Row xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
                <Cell xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
                    <Data ss:Type="String">Audit ID</Data>
                </Cell>
                <Cell xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
                    <Data ss:Type="String">Audit Subcategory ID</Data>
                </Cell>
                <Cell xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
                    <Data ss:Type="String">1</Data>
                </Cell>
                <Cell xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
                    <Data ss:Type="String">ObjectID</Data>
                </Cell>
                <Cell xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
                    <Data ss:Type="String">ObjectTypeID</Data>
                </Cell>
            </Row>
            <Row xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
                <Cell xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
                    <Data ss:Type="String">55406</Data>
                </Cell>
                <Cell xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
                    <Data ss:Type="String">3</Data>
                </Cell>
                <Cell xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
                    <Data ss:Type="String">1</Data>
                </Cell>
                <Cell xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
                    <Data ss:Type="String">6078</Data>
                </Cell>
                <Cell xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
                    <Data ss:Type="String">1</Data>
                </Cell>
            </Row>
        </Table>
    </Worksheet>
</Workbook>

我正在尝试使用以下代码插入节点:

SET @xml.modify('insert <Maintenance>111111111111111</Maintenance> into (/Workbook)[1]');

然后我使用

显示数据
  Select @xml;

问题是没有显示新节点。 我尝试使用

修改XML
SET @xml.modify('insert <Maintenance>111111111111111</Maintenance> into (/Workbook/Worksheet)[1]');

但是这也没有插入任何节点。

任何人都可以建议我做错了吗?

2 个答案:

答案 0 :(得分:2)

在插入时,似乎需要使用默认命名空间。试试这个。

set @xml.modify('
declare namespace ns="urn:schemas-microsoft-com:office:spreadsheet";
insert <ns:Maintenance>111111111111111</ns:Maintenance>
into (/ns:Workbook)[1]');


select @xml

答案 1 :(得分:0)

这个工作人员..

E: Could not open lock file /var/lib/dpkg/lock - open (13 Permission denied)
     E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?

我必须声明XML中使用的所有名称空间。 @xmlStyle是XML Type,包含我想要包含为节点的XML片段。

 SET @xml.modify('
        declare default element namespace  "urn:schemas-microsoft-com:office:spreadsheet";
        declare namespace ss="urn:schemas-microsoft-com:office:spreadsheet" ;
        declare namespace x="urn:schemas-microsoft-com:office:excel";
        insert sql:variable("@xmlStyle") as first into (/Workbook)[1]')