Arbeitsbeispiel zur Aktualisierung von XML-Knoten in einer Tabelle
create table xml (xml xml);
insert xml values('<customer name="John"><postcode>P22 2XH</postcode></customer>');
insert xml values('<customer name="Doe"><postcode>P22 2XH</postcode></customer>');
insert xml values('<customer name="Jane"><postcode>P9 2XH</postcode></customer>');
UPDATE xml
SET xml.modify('
replace value of (//customer/postcode[text()="P22 2XH"]/text())[1]
with "IP22 2XH" ');
select * from xml;
Wenn Sie mehrere Postleitzahlenknoten PER xml-record-column hatten, dann können Sie das unten stehende verwenden. SQL Server erlaubt nur eine xml-Knoten-Ersetzung pro modify
Sie müssen also eine Schleife machen.
create table xml (salesperson varchar(100), portfolios xml);
insert xml values('jim','
<customer name="John"><postcode>P22 2XH</postcode></customer>
<customer name="Doe"><postcode>P22 2XH</postcode></customer>
<customer name="Jane"><postcode>P9 2XH</postcode></customer>');
insert xml values('mary','
<customer name="Joe"><postcode>Other</postcode></customer>
<customer name="Public"><postcode>P22 2XH</postcode></customer>');
while exists (
select * from xml
cross apply portfolios.nodes('//customer/postcode[text()="P22 2XH"]') n(c))
UPDATE xml
SET portfolios.modify('
replace value of (//customer/postcode[text()="P22 2XH"]/text())[1]
with "IP22 2XH" ');
;
select * from xml