我需要更新SQL Server列中的XML节点。
我看到很多使用xpath并使用属性识别节点的例子。
我正在使用的XML是自动生成的,并没有在我需要的节点上提供属性。如何构造update语句以使用节点中的其他值来查找它?
我希望能写出这样的东西:
declare @xml xml ='
<Content>
<ContentItems>
<ContentItem>
<ContentKey>abc</ContentKey>
<Body>TextToUpdate</Body>
</ContentItem>
<ContentItem>
<ContentKey>efg</ContentKey>
<Body>Other</Body>
</ContentItem>
</ContentItems>
</Content>'
select @xml
set @xml.modify(
'
replace value of
(/content/contentitems[ContentKey="abc"]/body/text())[1]
with ("Success")')
select @xml
答案 0 :(得分:1)
你正朝着正确的方向前进。只有错误的字符大小写,如另一个答案中所述,并且在XPath中缺少/ContentItem
,阻止原始查询起作用。解决这个问题,你很高兴:
set @xml.modify('
replace value of
(/Content/ContentItems/ContentItem[ContentKey="abc"]/Body/text())[1]
with "Success"
')
答案 1 :(得分:0)
首先: XML区分大小写。因此,您需要确保在XPath中正确调用标记。
第二:看一下这个例子......
;with dateSequences(num, empId, orderDate) as
(
select ROW_NUMBER() over (order by orderdate) as num
, empId
, orderdate
from yourTable
),
dateGroups(groupNum, empId, orderDate, num) as
(
select currD.num, currD.empid, currD.orderDate, currD.num
from dateSequences currD
left join dateSequences prevD on prevD.num = currD.num - 1 and prevD.empid = currD.empId
where prevD.num is null
union all
select dg.groupNum, d.empId, d.orderDate, d.num
from dateSequences d
inner join dateGroups dg on dg.num + 1 = d.num and d.empId = dg.empId
)
select empId, min(orderDate) as MinDate, max(orderDate) as MaxDate
from dateGroups
where empId = 4
group by empId, groupNum