insert(XML DML)

적용 대상:SQL ServerAzure SQL DatabaseAzure SQL Managed Instance

Expression1로 식별되는 하나 이상의 노드를 Expression2로 식별된 노드의 자식 노드 또는 형제로 삽입합니다.

Syntax

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

참고 항목

SQL Server 2014(12.x) 및 이전 버전에 대한 Transact-SQL 구문을 보려면 이전 버전 설명서를 참조 하세요.

인수

Expression1
삽입할 노드를 하나 이상 식별합니다. 이 인스턴스는 상수 XML 인스턴스일 수 있습니다. 수정 메서드가 적용되는 동일한 XML 스키마 컬렉션의 형식화된 XML 데이터 형식 인스턴스에 대한 참조입니다. 독립 실행형 sql:column()/sql:variable() 함수 또는 XQuery 식을 사용하는 형식화되지 않은 XML 데이터 형식 인스턴스입니다. 식은 노드와 텍스트 노드 또는 순서가 지정된 노드 시퀀스로 발생할 수 있습니다. 루트(/) 노드로 확인할 수 없습니다. 식에서 값 또는 값 시퀀스가 생성되는 경우 값은 시퀀스의 각 값을 구분하는 공백이 있는 단일 텍스트 노드로 삽입됩니다. 여러 노드를 상수로 지정하면 노드가 괄호 안에 포함되고 쉼표로 구분됩니다. 요소, 특성 또는 값 시퀀스 같은 다른 유형의 시퀀스를 삽입할 수 없습니다. Expression1이 빈 시퀀스로 확인되면 삽입이 발생하지 않고 오류가 반환되지 않습니다.

into
Expression1에 의해 식별된 노드는 Expression2에 의해 식별된 노드의 직접 하위 항목(자식 노드)으로 삽입됩니다. Expression2의 노드에 이미 하나 이상의 자식 노드가 있는 경우 첫 번째 또는 마지막 노드를 사용하여 새 노드를 추가할 위치를 지정해야 합니다. 예를 들어 자식 목록의 시작 또는 끝에 각각 있습니다. 특성이 삽입되면 첫 번째마지막 키워드(keyword) 무시됩니다.

after
Expression1로 식별되는 노드는 Expression2로 식별된 노드 바로 뒤에 형제로 삽입됩니다. after 키워드(keyword) 특성을 삽입하는 데 사용할 수 없습니다. 예를 들어 특성 생성자를 삽입하거나 XQuery에서 특성을 반환하는 데 사용할 수 없습니다.

전에
Expression1로 식별되는 노드는 Expression2로 식별된 노드 바로 앞에 형제로 삽입됩니다. 특성을 삽입할 때는 이전 키워드(keyword) 사용할 수 없습니다. 예를 들어 특성 생성자를 삽입하거나 XQuery에서 특성을 반환하는 데 사용할 수 없습니다.

Expression2
노드를 식별합니다. Expression1에서 식별된 노드는 Expression2로 식별된 노드를 기준으로 삽입됩니다. 현재 참조된 문서에 있는 노드에 대한 참조를 반환하는 XQuery 식일 수 있습니다. 둘 이상의 노드가 반환되면 삽입이 실패합니다. Expression2가 빈 시퀀스를 반환하는 경우 삽입이 발생하지 않고 오류가 반환되지 않습니다. Expression2가 정적으로 단일 항목이 아닌 경우 정적 오류가 반환됩니다. Expression2 는 처리 명령, 주석 또는 특성일 수 없습니다. Expression2는 생성된 노드가 아니라 문서의 기존 노드에 대한 참조여야 합니다.

예제

A. 문서에 요소 노드 삽입

다음 예제에서는 문서에 요소를 삽입하는 방법을 보여 줍니다. 먼저 XML 문서가 xml 형식의 변수에 할당됩니다. 그런 다음, 여러 insert XML DML 문을 통해 예에서 요소 노드가 문서에 삽입되는 방법을 보여 줍니다. 각 삽입 후에 SELECT 문은 결과를 표시합니다.

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>Aluminium</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  

이 예제의 다양한 경로 식은 정적별 입력 요구 사항으로 "[1]"을 지정합니다. 이렇게 하면 단일 대상 노드가 보장됩니다.

B. 문서에 여러 요소 삽입

다음 예제에서는 xml 형식의 변수에 문서가 먼저 할당됩니다. 그런 다음 제품 기능을 나타내는 두 요소의 시퀀스가 xml 유형의 두 번째 변수에 할당됩니다. 그런 다음 이 시퀀스가 첫 번째 변수에 삽입됩니다.

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. 문서에 특성 삽입

다음 예제에서는 특성이 문서에 삽입되는 방법을 보여 줍니다. 먼저 문서가 xml 형식 변수에 할당됩니다. 그런 다음 일련의 XML DML 문을 사용하여 문서에 특성을 삽입합니다. 각 특성 삽입 이후 SELECT 문으로 결과를 표시합니다.

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. 메모 노드 삽입

이 쿼리에서 XML 문서는 먼저 xml 형식의 변수에 할당됩니다. 그런 다음, XML DML을 사용하여 첫 번째 <step> 요소 다음에 주석 노드를 삽입합니다.

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. 처리 명령 삽입

다음 쿼리에서 XML 문서는 먼저 xml 유형의 변수에 할당됩니다. 그런 다음 XML DML 키워드(keyword) 사용하여 문서 시작 부분에 처리 명령을 삽입합니다.

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 섹션을 사용하여 데이터 삽입

XML에서 유효하지 않은 < 또는 > 등의 문자가 포함된 텍스트를 삽입하는 경우 다음 쿼리와 같이 CDATA 섹션을 사용하여 데이터를 삽입할 수 있습니다. 쿼리는 CDATA 섹션을 지정하지만 잘못된 문자가 엔터티로 변환된 텍스트 노드로 추가됩니다. 예를 들어 . <&lt;저장됩니다.

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  

이 쿼리는 <Features> 요소에 텍스트 노드를 삽입합니다.

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

G. 텍스트 노드 삽입

이 쿼리에서 XML 문서는 먼저 xml 형식의 변수에 할당됩니다. 그런 다음, XML DML을 사용하여 <Root> 요소의 첫 번째 자식으로 텍스트 노드를 삽입합니다. 텍스트 생성자는 텍스트를 지정하는 데 사용됩니다.

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. 형식화되지 않은 xml 열에 새 요소 삽입

다음 예제에서는 XML DML을 적용하여 xml 형식 열에 저장된 XML 인스턴스를 업데이트합니다.

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>Aluminium</Material> as first  
  into   (/Root/ProductDescription/Features)[1]  
');  
GO  

마찬가지로, <Material> 요소 노드를 삽입하는 경우 경로 식에서 단일 대상을 반환해야 합니다. 이것은 식의 끝에 [1]을 추가하여 명시적으로 지정됩니다.

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

9\. if 조건 문을 기반으로 삽입

다음 예제에서는 XML DML 삽입 문에서 EXPRESSION1의 일부로 IF 조건을 지정합니다. 조건이 True이면 특성이 <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  

다음 예제는 XML DML 삽입 문이 조건이 True이면 문서에 요소를 삽입한다는 점을 제외하고 비슷합니다. 즉, <WorkCenter> 요소가 두 개의 <step> 자식 요소보다 작거나 같은 경우입니다.

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  

결과는 다음과 같습니다.

<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. 형식화된 xml 열에 노드 삽입

다음은 형식 화된 XML 열에 저장된 제조 지침 XML에 요소와 특성을 삽입하는 예제입니다.

이 예에서는 먼저 AdventureWorks 데이터베이스에서 형식화된 xml 열을 가진 테이블(T)을 만듭니다. 그런 다음 ProductModel 테이블의 Instructions 열에서 T 테이블로 제조 지침 XML 인스턴스를 복사합니다. 그런 다음 T 테이블의 XML에 삽입이 적용됩니다.

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             

참고 항목

형식화된 XML과 형식화되지 않은 XML 비교
XML 데이터의 인스턴스 만들기
xml 데이터 형식 메서드
XML DML(XML 데이터 수정 언어)