replace value of(XML DML)
적용 대상: SQL Server
Azure SQL 데이터베이스
Azure SQL Managed Instance
문서의 노드 값을 업데이트합니다.
Syntax
replace value of Expression1
with Expression2
참고 항목
SQL Server 2014(12.x) 이전 버전의 Transact-SQL 구문을 보려면 이전 버전 설명서를 참조하세요.
인수
Expression1
값을 업데이트할 노드를 식별합니다. 단일 노드만 식별해야 합니다. 즉, Expression1 은 정적 싱글톤이어야 합니다. XML을 입력하는 경우 노드의 형식은 단순 형식이어야 합니다. 여러 노드를 선택하면 오류가 발생합니다. Expression1이 빈 시퀀스를 반환하는 경우 값 대체가 발생하지 않고 오류가 반환되지 않습니다. Expression1 은 단순 형식 콘텐츠(목록 또는 원자성 형식), 텍스트 노드 또는 특성 노드가 있는 단일 요소를 반환해야 합니다. Expression1 은 공용 구조체 형식, 복합 형식, 처리 명령, 문서 노드 또는 주석 노드일 수 없거나 오류가 반환됩니다.
Expression2
노드의 새 값을 식별합니다. 암시적으로 사용되기 때문에 data()
단순 형식 노드를 반환하는 식일 수 있습니다. 값이 값 목록인 경우 문은 update
이전 값을 목록으로 바꿉니다. 형식화된 XML 인스턴스를 수정하는 경우 Expression2는 Expression1의 동일한 형식 또는 하위 형식이어야 합니다. 그렇지 않으면 오류가 반환됩니다. 형식화되지 않은 XML 인스턴스 를 수정하는 경우 Expression2 는 원자화할 수 있는 식이어야 합니다. 그렇지 않으면 오류가 반환됩니다.
예제
이 문서의 Transact-SQL 코드 샘플은 AdventureWorks2022
또는 AdventureWorksDW2022
샘플 데이터베이스를 사용하며, 이는 Microsoft SQL Server 예시 및 커뮤니티 프로젝트(Microsoft SQL Server Samples and Community Projects) 홈 페이지에서 다운로드할 수 있습니다.
XML DML 문의 다음 예제 replace value of
에서는 XML 문서에서 노드를 업데이트하는 방법을 보여 줍니다.
A. XML 인스턴스의 값 바꾸기
다음 예제에서는 먼저 문서 인스턴스가 xml 형식의 변수에 할당됩니다. 그런 다음 XML replace value of
DML 문이 문서의 값을 업데이트합니다.
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;
업데이트되는 대상은 식 끝에 "[1]"을 추가하여 경로 식에 명시적으로 지정된 하나의 노드여야 합니다.
B. if 식을 사용하여 대체 값 확인
다음 예제와 같이 문의 Expression2 replace value of
에서 식을 지정할 if
수 있습니다. Expression1은 LaborHours
첫 번째 작업 센터에서 업데이트할 특성을 식별합니다. Expression2 는 if
식을 사용하여 특성의 LaborHours
새 값을 결정합니다.
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. 형식화되지 않은 XML 열에 저장된 XML 업데이트
다음 예제에서는 열에 저장된 XML을 업데이트합니다.
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. 형식화된 XML 열에 저장된 XML 업데이트
다음 예에서는 형식화된 XML 열에 저장되어 있는 제조 지침 문서의 값을 대체합니다.
이 예제에서는 먼저 데이터베이스에 형식화된 XML 열이 있는 테이블(T
)을 AdventureWorks2022
만듭니다. 그런 다음, 테이블의 열에서 제조 지침 XML 인스턴스를 Instructions
ProductModel
테이블 T
로 복사합니다. 그런 다음 삽입이 테이블 T
의 XML에 적용됩니다.
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;
이 예제에서는 값을 바꿀 LotSize
때 사용합니다cast
. cast
는 값이 특정 형식이어야 하는 경우 필요합니다. 이 예제에서 값이면 500
명시적 캐스팅이 필요하지 않습니다.
관련 콘텐츠
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기