Aracılığıyla paylaş


(XML DML) değerini değiştir

Şunlar için geçerlidir:SQL ServerAzure SQL VeritabanıAzure SQL Yönetilen ÖrneğiMicrosoft Fabric'te SQL veritabanı

Belgedeki bir düğümün değerini güncelliyor.

Sözdizimi

replace value of Expression1
with Expression2

Arguments

İfade1

Değeri güncellenecek bir düğümü belirler. Yalnızca tek bir düğümü tanımlaması gerekir. Yani, Expression1 statik bir tekli olmalıdır. XML tiplenmişse, düğümün tipi basit bir tip olmalıdır. Birden fazla düğüm seçildiğinde hata oluşur. Expression1 boş bir dizi döndürürse, değer değişimi olmaz ve hata döner. Expression1, basit tür içeriğine (liste veya atomik tipler), bir metin düğümü veya bir öznitelik düğümü olan tek bir eleman döndürmelidir. Expression1 bir birlik tipi, karmaşık tip, işleme talimatı, belge düğümü veya yorum düğümü olamaz, yoksa hata döndürülebilir.

İfade2

Düğümün yeni değerini belirler. Basit bir tip düğüm döndüren bir ifade olabilir, çünkü data() örtük olarak kullanılır. Eğer değer bir değer listesi ise, ifade update eski değeri listeyle değiştirir. Tiplenmiş bir XML örneğini değiştirdiğinizde, Expression2 aynı tip veya Expression1'in alt tipi olmalıdır. Aksi takdirde bir hata döner. Tiplenmemiş bir XML örneğini değiştirdiğinizde, Expression2 atomize edilebilen bir ifade olmalıdır. Aksi takdirde bir hata döner.

Örnekler

Bu makaledeki kod örnekleri, AdventureWorks2025 giriş sayfasından indirebileceğiniz AdventureWorksDW2025 veya örnek veritabanını kullanır.

Aşağıdaki XML DML ifadesi örnekleri replace value of , bir XML belgesinde düğümlerin nasıl güncelleneceğini göstermektedir.

A. Bir XML örneğinde değerleri değiştirin

Aşağıdaki örnekte, bir belge örneği önce xml tipinde bir değişkene atanmıştır. Daha sonra, replace value of XML DML ifadeleri belgelerdeki değerleri günceller.

DECLARE @myDoc XML;

SET @myDoc = '<Root>
<Location LocationID="10"
            LaborHours="1.1"
            MachineHours=".2" >Manufacturing steps are described here.
<step>Manufacturing step 1 at this work center</step>
<step>Manufacturing step 2 at this work center</step>
</Location>
</Root>';

SELECT @myDoc;

-- update text in the first manufacturing step
SET @myDoc.modify('
  replace value of (/Root/Location/step[1]/text())[1]
  with "new text describing the manu step"
');

SELECT @myDoc;

-- update attribute value
SET @myDoc.modify('
  replace value of (/Root/Location/@LaborHours)[1]
  with "100.0"
');

SELECT @myDoc;

Güncellenen hedef, en fazla, yol ifadesinde ifadenin sonuna "[1]" eklenerek açıkça belirtilmiş bir düğüm olmalıdır.

B. Yerine değeri belirlemek için if ifadesini kullanın

Aşağıdaki örnekte gösterildiği gibi, ifadenin ifİfade 2'sinde ifade ifade edilebilirreplace value of. Expression1, güncellenecek ilk çalışma merkezinden gelen özniteliği tanımlar LaborHours . Expression2, özniteliğin if yeni değerini belirlemek için bir LaborHours ifade kullanır.

DECLARE @myDoc XML;
SET @myDoc = '<Root>
<Location LocationID="10"
            LaborHours=".1"
            MachineHours=".2" >Manu steps are described here.
<step>Manufacturing step 1 at this work center</step>
<step>Manufacturing step 2 at this work center</step>
</Location>
</Root>';

--SELECT @myDoc
SET @myDoc.modify('
  replace value of (/Root/Location[1]/@LaborHours)[1]
  with (
       if (count(/Root/Location[1]/step) > 3) then
         "3.0"
       else
          "1.0"
      )
');

SELECT @myDoc;

C. Tiplenmemiş bir XML sütununda saklanan XML'i güncelle

Aşağıdaki örnek, bir sütunda depolanan XML'yi günceller:

DROP TABLE T;
GO

CREATE TABLE T (i INT, x XML);
GO

INSERT INTO T
VALUES (
    1,
    '<Root>
<ProductDescription ProductID="1" ProductName="Road Bike">
<Features>
  <Warranty>1 year parts and labor</Warranty>
  <Maintenance>3 year parts and labor extended maintenance is available</Maintenance>
</Features>
</ProductDescription>
</Root>'
);
GO

-- verify the current <ProductDescription> element
SELECT x.query(' /Root/ProductDescription')
FROM T;

-- update the ProductName attribute value
UPDATE T
SET x.modify('
  replace value of (/Root/ProductDescription/@ProductName)[1]
  with "New Road Bike" ');

-- verify the update
SELECT x.query(' /Root/ProductDescription');
FROM T

D. Tiplenmiş XML sütununda saklanan XML'i güncelle

Bu örnek, bir üretim talimatları belgesindeki değerleri bir XML sütununda depolanmış olarak değiştirir.

Örnekte, önce veritabanında bir XML sütunuyla T bir tablo (AdventureWorks2025) oluşturursunuz. Daha sonra tablodaki Instructions sütundan ProductModel bir üretim talimatları XML örneğini tabloya Tkopyalarsınız. Eklemeler daha sonra tabloda TXML'ye uygulanır.

USE AdventureWorks2022;
GO

DROP TABLE T;
GO

CREATE TABLE T (
    ProductModelID INT PRIMARY KEY,
    Instructions XML(Production.ManuInstructionsSchemaCollection)
);
GO

INSERT T
SELECT ProductModelID, Instructions
FROM Production.ProductModel
WHERE ProductModelID = 7;
GO

--insert a new location - <Location 1000/>.
UPDATE T
SET Instructions.modify('
  declare namespace MI="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";
insert <MI:Location LocationID="1000"  LaborHours="1000"  LotSize="1000" >
           <MI:step>Do something using <MI:tool>hammer</MI:tool></MI:step>
         </MI:Location>
  as first
  into (/MI:root)[1]
');
GO

SELECT Instructions
FROM T;
GO

-- Now replace manu. tool in location 1000
UPDATE T
SET Instructions.modify('
  declare namespace MI="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";
  replace value of (/MI:root/MI:Location/MI:step/MI:tool)[1]
  with "screwdriver"
');
GO

SELECT Instructions
FROM T;

-- Now replace value of lot size
UPDATE T
SET Instructions.modify('
  declare namespace MI="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";
  replace value of (/MI:root/MI:Location/@LotSize)[1]
  with 500 cast as xs:decimal ?
');
GO

SELECT Instructions
FROM T;

Bu örnek, değer değiştirirken cast kullanılırLotSize. cast değerin belirli bir türde olması gerektiğinde gereklidir. Bu örnekte, 500 eğer değer ise, açık bir kast yapmak gerekmez.