如何更新sql server中的xml列中的元素名称

时间:2021-12-04 07:24:52

I have an xml column in my table like following:

我的表中有一个xml列,如下所示:

   <info>
      <teacher>     
            <name>John</name>   
      </teacher>
      <StInfo>
        <name>William</name>    
        <address>India</address>    
      </StInfo>
    </info>

I have to just update to

我得更新一下

 <info>
          <teacher>     
                <name>John</name>   
          </teacher>
          <Student>
            <name>William</name>    
            <address>India</address>    
          </Student>
        </info>

2 个解决方案

#1


4  

I've built an example for you in SQL Server using your data:

我用您的数据为您在SQL Server中构建了一个示例:

DECLARE @StackExample TABLE (
     Id UNIQUEIDENTIFIER NOT NULL PRIMARY KEY DEFAULT(NEWID())
    ,XmlColumn XML
)

INSERT INTO @StackExample(XmlColumn) VALUES('<info>
  <StInfo>
    <name>William</name>    
    <address>India</address>    
  </StInfo>
</info>')


UPDATE T
SET XmlColumn = XmlColumn.query('<info>
  <Student>
  {info/StInfo/*}
  </Student>
</info>')
FROM @StackExample t

SELECT * FROM @StackExample

I hope this can help

我希望这能有所帮助

#2


0  

As far as I know, there's no way to modify xml element names with SQL DML. You can replace it after converting to varchar:

据我所知,没有办法用SQL DML修改xml元素名称。您可以在转换到varchar后替换它:

update Table1 set
    data = cast(
      replace(
        replace(
          cast(data as nvarchar(max)),
        '</StInfo>', '</Student>'
        ),
      '<StInfo>', '<Student>'
      )
    as xml)

Or you can reconstruct your xml using XQuery:

或者可以使用XQuery重构xml:

update Table1 set
    data = data.query('<info>{
                for $i in info/StInfo
                return element Student {$i/*}
                }</info>'
    )

These appoaches will work even for multiple elements.

这些appoaches甚至可以用于多个元素。

sql fiddle demo

sql小提琴演示

#1


4  

I've built an example for you in SQL Server using your data:

我用您的数据为您在SQL Server中构建了一个示例:

DECLARE @StackExample TABLE (
     Id UNIQUEIDENTIFIER NOT NULL PRIMARY KEY DEFAULT(NEWID())
    ,XmlColumn XML
)

INSERT INTO @StackExample(XmlColumn) VALUES('<info>
  <StInfo>
    <name>William</name>    
    <address>India</address>    
  </StInfo>
</info>')


UPDATE T
SET XmlColumn = XmlColumn.query('<info>
  <Student>
  {info/StInfo/*}
  </Student>
</info>')
FROM @StackExample t

SELECT * FROM @StackExample

I hope this can help

我希望这能有所帮助

#2


0  

As far as I know, there's no way to modify xml element names with SQL DML. You can replace it after converting to varchar:

据我所知,没有办法用SQL DML修改xml元素名称。您可以在转换到varchar后替换它:

update Table1 set
    data = cast(
      replace(
        replace(
          cast(data as nvarchar(max)),
        '</StInfo>', '</Student>'
        ),
      '<StInfo>', '<Student>'
      )
    as xml)

Or you can reconstruct your xml using XQuery:

或者可以使用XQuery重构xml:

update Table1 set
    data = data.query('<info>{
                for $i in info/StInfo
                return element Student {$i/*}
                }</info>'
    )

These appoaches will work even for multiple elements.

这些appoaches甚至可以用于多个元素。

sql fiddle demo

sql小提琴演示