【问题标题】:Unable to insert new node using insert (XML DML)无法使用插入 (XML DML) 插入新节点
【发布时间】:2015-12-08 16:59:04
【问题描述】:

我正在尝试使用 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]');

但这也不会插入任何节点。

谁能建议我做错了什么?

【问题讨论】:

    标签: sql sql-server tsql xml-dml


    【解决方案1】:

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

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

    【讨论】:

    • @NimbleFungus 这很奇怪,因为它对我有用,它出现在 Worksheet 元素的下方。我会尝试用 sql fiddle 来解决它。
    • 请看我的回答。我正在使用 SQL Server 2008 R2。版本会影响行为吗?但是非常感谢你的帮助。你回答游戏我的想法尝试我的答案。
    【解决方案2】:

    这工作了伙计们..

     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]')
    

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

    @xmlStyle AS XML
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-18
      • 1970-01-01
      • 1970-01-01
      • 2018-11-28
      • 1970-01-01
      • 2020-10-17
      相关资源
      最近更新 更多