如何使用 TSQL 将属性添加到现有根 XML 节点?

2024-01-11

我已经用 SQL 构建了一些 XML。

declare @requestXML xml

set @requestXML = (
select @dataXML
for xml raw ('rtEvent') 

我现在的一般输出遵循与此类似的模式:

<rtEvent>
  <ctx>
    .....
  </ctx>
</rtEvent>

我现在想做的是将一些属性和值添加到 rtEvent 根 元素节点,但我不确定如何实现它。

我查看了 XML 对象的修改方法,并观察了插入、替换值和删除操作,但似乎无法弄清楚如何使用它们中的任何一个来实现我想要的结果。

基本上,我希望能够修改根节点以反映如下内容:

<rtEvent type="customType" email="[email protected] /cdn-cgi/l/email-protection"
  origin="eCommerce" wishedChannel="0" externalId="5515">
   <ctx>
     ...
   </ctx>
</rtEvent>

我应该使用记录的 XML.Modify 还是有更好的方法?应该怎么做呢?


以防万一您想查看修改方法:

DECLARE @requestXML XML = '<rtEvent><ctx>...</ctx></rtEvent>'
SET @requestXML.modify(
    'insert 
    (
        attribute type {"customeType"},
        attribute email {"[email protected] /cdn-cgi/l/email-protection"},
        attribute origin {"eCommerce"},
        attribute wishedChannel {"0"},
        attribute externalId {"5515"}
    )
    into (/rtEvent)[1]')
SELECT @requestXML

它返回这个:

<rtEvent type="customeType" email="[email protected] /cdn-cgi/l/email-protection" origin="eCommerce" wishedChannel="0" externalId="5515">
  <ctx>...</ctx>
</rtEvent>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 TSQL 将属性添加到现有根 XML 节点? 的相关文章

随机推荐