Aracılığıyla paylaş


ekle (XML DML)

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

Expression1 tarafından tanımlanan bir veya daha fazla düğümü Expression2 tarafından tanımlanan düğümün kardeşleri olarak tanımlanan bir veya daha fazla düğümü ekler.

Sözdizimi

insert Expression1 (  
{as first | as last} into | after | before  
Expression2  
)  

Arguments

İfade1
Bir veya daha fazla düğümü yerleştirmek için tanımlar. Bu sabit bir XML örneği olabilir; modifiye yönteminin uygulandığı aynı XML Şema koleksiyonunun tiplenmiş XML veri tipi örneğine bir referans; bağımsız bir sql:column()/sql:variable() fonksiyonu kullanan tipsiz bir XML veri tipi örneği; veya bir XQuery ifadesi. Bu ifade bir düğüm, ayrıca bir metin düğümü veya sıralı bir düğüm dizisi oluşturabilir. Kök (/) düğümüne çözüm bulamaz. Eğer ifade bir değer veya değer dizisi çıkarsa, değerler tek bir metin düğümü olarak eklenir ve dizideki her değeri ayıran bir boşluk bulunur. Birden fazla düğümü sabit olarak belirtirseniz, düğümler parantez içinde yer alır ve virgülle ayrılır. Elementler, özellikler veya değerler gibi heterojen diziler ekleyemezsiniz. Expression1 boş bir diziye çözümlenirse, ekleme olmaz ve hata döner.

içine
Expression1 ile tanımlanan düğümler, Expression2 tarafından tanımlanan düğümün doğrudan torunları (çocuk düğümleri) olarak eklenir. Expression2'deki düğümde zaten bir veya daha fazla çocuk düğüm varsa, yeni düğümün nereye ekleneceğini belirtmek için ya birinci ya da son olarak kullanmalısınız. Örneğin, çocuk listesinin başında veya sonunda sırasıyla vardır. Özellik eklendiğinde ilk ve son anahtar kelimeler göz ardı edilir.

after
Expression1 ile tanımlanan düğümler, Expression2 tarafından tanımlanan düğümden hemen sonra kardeş olarak eklenir. After anahtar kelimesi öznitelik eklemek için kullanılamaz. Örneğin, bir öznitelik oluşturucu eklemek veya bir XQuery'den bir öznitelik döndürmek için kullanılamaz.

before
Expression1 ile tanımlanan düğümler, Expression2 tarafından tanımlanan düğümün hemen önüne kardeş olarak eklenir. Öncesi anahtar kelimesi, öznitelikler eklenirken kullanılamaz. Örneğin, bir öznitelik oluşturucu eklemek veya bir XQuery'den bir öznitelik döndürmek için kullanılamaz.

İfade2
Bir düğümü belirler. Expression1'de tanımlanan düğümler, Expression2 tarafından tanımlanan düğüme göre eklenir. Bu, şu anda referans edilen belgede var olan bir düğüme referans döndüren bir XQuery ifadesi olabilir. Birden fazla düğüm dönerse, ekleme başarısız olur. Expression2 boş bir dizi döndürürse, ekleme olmaz ve hata döner. Expression2 statik olarak singleton değilse, statik bir hata döner. Expression2, işleme komutu, yorum veya öznitelik olamaz. Belirtmek gerekir ki, Expression2 belgedeki mevcut bir düğüme referans olmalı, oluşturulmuş bir düğüm olmamalıdır.

Örnekler

A. Belgeye eleman düğümleri eklenmesi

Aşağıdaki örnek, bir belgeye eleman nasıl ekleneceğini göstermektedir. İlk olarak, bir XML belgesi xml tipinde bir değişkene atanır. Daha sonra, birkaç insert XML DML ifadesi sayesinde, örnek öğe düğümlerinin belgeye nasıl eklendiğini gösterir. Her eklemeden sonra, SELECT ifadesi sonucu gösterir.

USE AdventureWorks;  
GO  
DECLARE @myDoc XML;         
SET @myDoc = '<Root>         
    <ProductDescription ProductID="1" ProductName="Road Bike">         
        <Features>         
        </Features>         
    </ProductDescription>         
</Root>'  ;       
SELECT @myDoc;     
-- insert first feature child (no need to specify as first or as last)         
SET @myDoc.modify('         
insert <Maintenance>3 year parts and labor extended maintenance is available</Maintenance>   
into (/Root/ProductDescription/Features)[1]') ;  
SELECT @myDoc ;        
-- insert second feature. We want this to be the first in sequence so use 'as first'         
SET @myDoc.modify('         
insert <Warranty>1 year parts and labor</Warranty>          
as first         
into (/Root/ProductDescription/Features)[1]         
')  ;       
SELECT @myDoc  ;       
-- insert third feature child. This one is the last child of <Features> so use 'as last'         
SELECT @myDoc         
SET @myDoc.modify('         
insert <Material>Aluminum</Material>          
as last         
into (/Root/ProductDescription/Features)[1]         
')         
SELECT @myDoc ;        
-- Add fourth feature - this time as a sibling (and not a child)         
-- 'after' keyword is used (instead of as first or as last child)         
SELECT @myDoc  ;       
SET @myDoc.modify('         
insert <BikeFrame>Strong long lasting</BikeFrame>   
after (/Root/ProductDescription/Features/Material)[1]         
')  ;       
SELECT @myDoc;  
GO  

Bu örnekteki çeşitli yol ifadelerinin statik tipleme gereksinimi olarak "[1]" belirttiğini unutmayın. Bu, tek bir hedef düğümü sağlar.

B. Belgeye birden fazla öğe ekleme

Aşağıdaki örnekte, bir belge önce xml tipinde bir değişkene atanır. Daha sonra, ürün özelliklerini temsil eden iki elemanlık bir dizi xml tipinde ikinci bir değişkene atanır. Bu dizi daha sonra ilk değişkene eklenir.

USE AdventureWorks;  
GO  
DECLARE @myDoc XML;  
SET @myDoc = N'<Root>             
<ProductDescription ProductID="1" ProductName="Road Bike">             
    <Features> </Features>             
</ProductDescription>             
</Root>';  
DECLARE @newFeatures xml;  
SET @newFeatures = N'<Warranty>1 year parts and labor</Warranty>            
<Maintenance>3 year parts and labor extended maintenance is available</Maintenance>';           
-- insert new features from specified variable            
SET @myDoc.modify('             
insert sql:variable("@newFeatures")             
into (/Root/ProductDescription/Features)[1] ')             
SELECT @myDoc;  
GO  

C. Bir belgeye öznitelik ekleme

Aşağıdaki örnek, bir belgeye özniteliklerin nasıl eklendiğini göstermektedir. İlk olarak, bir belge xml tip değişkenine atanır. Daha sonra, belgeye öznitelikler eklemek için bir dizi XML DML ifadesi kullanılır. Her öznitelik eklendikten sonra, SELECT ifadesi sonucu gösterir.

USE AdventureWorks;  
GO  
DECLARE @myDoc XML;            
SET @myDoc =   
'<Root>             
    <Location LocationID="10" >             
        <step>Manufacturing step 1 at this work center</step>             
        <step>Manufacturing step 2 at this work center</step>             
    </Location>             
</Root>' ;  
SELECT @myDoc;          
-- insert LaborHours attribute             
SET @myDoc.modify('             
insert attribute LaborHours {".5" }             
into (/Root/Location[@LocationID=10])[1] ');           
SELECT @myDoc;          
-- insert MachineHours attribute but its value is retrieved from a sql variable @Hrs             
DECLARE @Hrs FLOAT;            
SET @Hrs =.2;          
SET @myDoc.modify('             
insert attribute MachineHours {sql:variable("@Hrs") }             
into   (/Root/Location[@LocationID=10])[1] ');            
SELECT @myDoc;             
-- insert sequence of attribute nodes (note the use of ',' and ()              
-- around the attributes.             
SET @myDoc.modify('             
insert (              
           attribute SetupHours {".5" },             
           attribute SomeOtherAtt {".2"}             
        )             
into (/Root/Location[@LocationID=10])[1] ');             
SELECT @myDoc;  
GO  

D. Bir yorum düğümü ekleme

Bu sorguda, bir XML belgesi önce xml tipinde bir değişkene atanır. Daha sonra, ilk <step> elemanın ardından bir yorum düğümü eklemek için XML DML kullanılır.

USE AdventureWorks;  
GO  
DECLARE @myDoc XML;             
SET @myDoc =   
'<Root>             
    <Location LocationID="10" >             
        <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('             
insert <!-- some comment -->             
after (/Root/Location[@LocationID=10]/step[1])[1] ');            
SELECT @myDoc;  
GO  

E. İşleme komutu ekleme

Aşağıdaki sorguda, bir XML belgesi önce xml tipinde bir değişkene atanır. Daha sonra, belgenin başına işleme komutu eklemek için XML DML anahtar kelimesi kullanılır.

USE AdventureWorks;  
GO  
DECLARE @myDoc XML;  
SET @myDoc =   
'<Root>   
    <Location LocationID="10" >   
        <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('   
insert <?Program = "Instructions.exe" ?>   
before (/Root)[1] ') ;  
SELECT @myDoc ;  
GO  

F. CDATA bölümü kullanılarak veri ekleme

XML'de geçerli olmayan karakterler içeren metin eklediğinizde, örneğin < veya >, CDATA bölümlerini kullanarak veriyi aşağıdaki sorguda gösterildiği gibi ekleyebilirsiniz. Sorgu bir CDATA bölümü belirtir, ancak geçersiz karakterler varlığa dönüştürülerek metin düğümü olarak eklenir. Örneğin, < . &lt;olarak kaydedilir.

USE AdventureWorks;  
GO  
DECLARE @myDoc XML;             
SET @myDoc =   
'<Root>             
    <ProductDescription ProductID="1" ProductName="Road Bike">             
        <Features> </Features>             
    </ProductDescription>             
</Root>' ;            
SELECT @myDoc ;            
SET @myDoc.modify('             
insert <![CDATA[ <notxml> as text </notxml> or cdata ]]>   
into  (/Root/ProductDescription/Features)[1] ') ;   
SELECT @myDoc ;  
GO  

Sorgu, öğeye bir metin düğümü <Features> ekler:

<Root>  
<ProductDescription ProductID="1" ProductName="Road Bike">  
<Features> &lt;notxml@gt; as text &lt;/notxml&gt; or cdata </Features>  
</ProductDescription>  
</Root>       

G. Metin düğümü ekleme

Bu sorguda, bir XML belgesi önce xml tipinde bir değişkene atanır. Daha sonra, öğenin <Root> ilk çocuğu olarak bir metin düğümü eklemek için XML DML kullanılır. Metin oluşturucu metni belirtmek için kullanılır.

USE AdventureWorks;  
GO  
DECLARE @myDoc XML;  
SET @myDoc = '<Root>  
<ProductDescription ProductID="1" ProductName="Road Bike">  
<Features>  
  
</Features>  
</ProductDescription>  
</Root>'  
SELECT @myDoc;  
SET @myDoc.modify('  
 insert text{"Product Catalog Description"}   
 as first into (/Root)[1]  
');  
SELECT @myDoc;  

H. Tiplenmemiş bir xml sütununa yeni bir eleman eklemek

Aşağıdaki örnek, XML DML'yi bir xml tipi sütunda depolanan bir XML örneğini güncellemek için uygular:

USE AdventureWorks;  
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  
-- insert a new element  
UPDATE T  
SET x.modify('insert <Material>Aluminum</Material> as first  
  into   (/Root/ProductDescription/Features)[1]  
');  
GO  

Yine, eleman <Material> düğümü eklendiğinde, yol ifadesi tek bir hedef döndürmelidir. Bu, ifadenin sonuna bir [1] eklenerek açıkça belirtilir.

-- check the update  
SELECT x.query(' //ProductDescription/Features')  
FROM T;  
GO  

I. If koşulu ifadesine dayalı ekleme

Aşağıdaki örnekte, insert XML DML ifadesinde Expression1'in bir parçası olarak bir IF koşulu belirtilmiştir. Eğer koşul Doğruysa, elemana bir özellik eklenir <WorkCenter> .

USE AdventureWorks;  
GO  
DECLARE @myDoc XML;  
SET @myDoc =   
'<Root>  
    <Location LocationID="10" LaborHours="1.2" >  
        <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('  
insert  
if (/Root/Location[@LocationID=10])  
then attribute MachineHours {".5"}  
else ()  
    as first into   (/Root/Location[@LocationID=10])[1] ');  
SELECT @myDoc;  
GO  

Aşağıdaki örnek benzerdir, ancak insert XML DML ifadesi koşul True ise belgeye bir eleman ekler. Yani, elemanın <WorkCenter> iki <step> alt elemandan daha az veya eşit olması durumunda.

USE AdventureWorks;  
GO  
DECLARE @myDoc XML;  
SET @myDoc =   
'<Root>  
    <Location LocationID="10" LaborHours="1.2" >  
        <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('  
insert  
if (count(/Root/Location/step) <= 2)  
then element step { "This is a new step" }  
else ()  
    as last into   (/Root/Location[@LocationID=10])[1] ');  
SELECT @myDoc;  
GO  

Sonuç şu şekildedir:

<Root>  
 <WorkCenter WorkCenterID="10" LaborHours="1.2">  
  <step>Manufacturing step 1 at this work center</step>  
  <step>Manufacturing step 2 at this work center</step>  
  <step>This is a new step</step>  
 </WorkCenter>  

J. Tiplenmiş bir xml sütununa düğüm ekleme

Bu örnek, bir üretim talimatları XML'ye bir öğe ve bir öznitelik eklenir; bu komut bir xml sütununda saklanır.

Örnekte, önce AdventureWorks veritabanında bir xml sütunuyla bir tablo (T) oluşturursunuz. Daha sonra ProductModel tablosunda Talimatlar sütunundan üretim talimatları XML örneğini tablo T'ye kopyalarsınız. Eklemeler daha sonra tablo T'deki XML'ye 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             
SELECT Instructions             
FROM T;  
-- now insertion begins             
--1) insert a new manu. Location. The <Root> specified as              
-- expression 2 in the insert() must be singleton.      
UPDATE T   
SET Instructions.modify('   
declare namespace MI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";   
insert <MI:Location LocationID="1000" >   
           <MI:step>New instructions go here</MI:step>   
         </MI:Location>   
as first   
into   (/MI:root)[1]   
') ;  
  
SELECT Instructions             
FROM T ;  
-- 2) insert attributes in the new <Location>             
UPDATE T             
SET Instructions.modify('             
declare namespace MI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";             
insert attribute LaborHours { "1000" }             
into (/MI:root/MI:Location[@LocationID=1000])[1] ');   
GO             
SELECT Instructions             
FROM T ;  
GO             
--cleanup             
DROP TABLE T ;  
GO             

Ayrıca Bkz.

Türlü XML'i Türsüz XML ile Karşılaştır
XML Verisi Örnekleri Oluşturmak
xml Veri Türü Yöntemleri
XML Veri Değiştirme Dili (XML DML)