Aracılığıyla paylaş


(xml dml) değerini değiştirin

Belge içindeki bir düğümün değerini güncelleştirir.

Sözdizimi

replace value of 
      Expression1 
with
      Expression2

Bağımsız değişkenler

  • Expression1
    Güncelleştirilecek değeri olan bir düğümü tanımlar. Yalnızca tek bir düğüm tanımlamak gerekir. Yani, Expression1Makine'ye tek olmalı. xml yazdıysanız, düğüm türü basit tür olmalıdır. Birden çok düğüm seçili ise, bir hata oluşturulur. Eğer Expression1döner boş sırası, hiçbir değer değişimi gerçekleşir ve herhangi bir hata döndürülür. Expression1sadece içeriği (liste veya atomik türleri), metin düğümü veya öznitelik düğümü yazmış tek bir öğe döndürmelidir. Expression1birleşim türü, bir karmaşık tür, bir işlem yönergesi, belge düğümü veya bir açıklama düğümü olamaz. Bu bir hata döndürülür.

  • Expression2
    Düğüm değeri tanımlar. Çünkü bu sadece yazılı bir düğümü döndüren deyim olabilir data() örtülü olarak kullanılacaktır. Değer bir değerler listesi ise güncelleştirmek deyimi değiştirir eski değer listesi. Yazılan bir xml örneği değiştirme içinde Expression2aynı tür veya alt türü olan Expression1. Aksi takdirde hata oluşur. Yazılmamış xml örneği, değiştirilmesi de Expression2atomized bir ifade olmalıdır. Aksi takdirde hata oluşur.

Örnekler

Aşağıdaki örnek değeri yerine xml dml deyimi xml belgesindeki düğümleri güncelleştirme gösterilmektedir.

A.Bir xml örneği değerleri değiştirme

Aşağıdaki örnekte, bir belge örneği ilk kez bir değişkene atanır xml türü. Daha sonra değeri yerine xml dml deyimlerini belge değerleri güncelleştirin.

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

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

Hedef güncelleştirilmekte, en yol ifadesi "[1]" ekleyerek açıkça belirtilen bir düğüm olması gerektiğini unutmayın ifadenin sonunda.

B.Eğer kullanarak ifade değiştirme değerini belirlemek için

Belirleyebileceğiniz , İfade2 ifade xml dml değeri yerine deyimi aşağıdaki örnekte gösterildiği gibi. İfade1 ilk iş merkezinden LaborHours özniteliği güncelleştirilir olduğunu tanımlar. İfade2 kullanan bir , LaborHours öznitelik değeri belirlemek için ifade.

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

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.Yazılmamış xml sütunda depolanan xml güncelleştiriliyor

Aşağıdaki örnek, bir sütunda depolanan xml güncelleştirir:

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

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.Yazılı xml sütunda depolanan xml güncelleştiriliyor

Bu örnek, bir üretim yönergeleri değerlerini değiştirir yazılan bir xml sütunda depolanan belge.

Örnek, sen önce bir tablo (t) yazılı xml sütun AdventureWorks veritabanındaki oluşturun. Kopyalamadan sonra bir üretim yönergeleri xml örneği ProductModel tablosundaki yönergeleri sütun tablo t. Eklemeler için xml tablosuna t. uygulanır

use AdventureWorks
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="https://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="https://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="https://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

use AdventureWorks
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="https://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="https://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="https://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

Kullanın döküm LotSize değerini değiştirirken. Belirli türde bir değer olmalıdır bu gereklidir. Bu örnekte, açık döküm, 500 değer, eğer gerekli olmaz.

Ayrıca bkz.

Kavramlar

Yazılı xml yazılmamış xml karşılaştırın

xml veri örnekleri oluşturma

xml veri değişikliği dili (xml dml)

Diğer Kaynaklar

XML veri türü yöntemleri