【问题标题】:Using sql to insert an xml attribute into an xml file使用sql将xml属性插入xml文件
【发布时间】:2014-04-30 01:30:05
【问题描述】:

使用 Microsoft SQL Server Management Studio,我在 xml 文件中插入了一个元素,现在希望插入一个属性,如下所示:

<a><b c="2"/></a>

我通过以下方式创建了b

update #tmp set column.modify('insert <b/> into (/a)[1]')

现在我希望创建c 作为b 的属性,如上所示。所以我尝试:

update #tmp set column.modify('insert attribute c{''2''} into (/a/@b)[1]')

但我得到了错误:

XQuery [#tmp.column.modify()]: The target of 'insert into' must be an element/document node, found 'attribute(b,xdt:untypedAtomic)

【问题讨论】:

    标签: sql-server xml insert


    【解决方案1】:

    b 是一个元素,而不是一个属性,因此您需要删除 @

    update #tmp
      set [column].modify('insert attribute c{''2''} into (/a/b)[1]');
    

    SqlFiddle here

    【讨论】:

    • 它有效,谢谢。我认为 c 是一个属性吗?我让他们翻转还是什么?我错过了什么?我以为b 是元素,里面的东西是属性?
    • c 是一个属性,b 是一个元素。 into 位需要一个元素目标(显然我们不能拥有带有属性的属性 :-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-31
    • 1970-01-01
    相关资源
    最近更新 更多