(xml dml) silme
Bir xml örneği düğümleri siler.
Sözdizimi
delete Expression
Bağımsız değişkenler
- Expression
XQuery ifade olduğunu belirten düğümleri silinecek. İfade ve ayrıca tüm düğümleri veya seçili düğüm içinde bulunan değerleri seçilen tüm düğümleri silinir. Açıklandığı gibi (xml dml) Ekle, bu başvuru belgesinde mevcut bir düğüm olması gerekir. Bu inşa düğüm olamaz. İfade, kök (/) olamaz. İfade boş sırası dönerse, hiçbir silme işlemi gerçekleşir ve herhangi bir hata döndürülmez.
Örnekler
A.Yazılmamış xml değişkeni içinde depolanan bir belgeden silme
Aşağıdaki örnek, çeşitli düğümleri belgeden silme verilmektedir. İlk olarak, bir xml örneği değişkene atanan xml türü. Ardından, sonraki silme xml dml deyimlerini çeşitli düğümleri belgeden silin.
DECLARE @myDoc xml
SET @myDoc = '<?Instructions for=TheWC.exe ?>
<Root>
<!-- instructions for the 1st work center -->
<Location LocationID="10"
LaborHours="1.1"
MachineHours=".2" >Some text 1
<step>Manufacturing step 1 at this work center</step>
<step>Manufacturing step 2 at this work center</step>
</Location>
</Root>'
SELECT @myDoc
-- delete an attribute
SET @myDoc.modify('
delete /Root/Location/@MachineHours
')
SELECT @myDoc
-- delete an element
SET @myDoc.modify('
delete /Root/Location/step[2]
')
SELECT @myDoc
-- delete text node (in <Location>
SET @myDoc.modify('
delete /Root/Location/text()
')
SELECT @myDoc
-- delete all processing instructions
SET @myDoc.modify('
delete //processing-instruction()
')
SELECT @myDoc
DECLARE @myDoc xml
SET @myDoc = '<?Instructions for=TheWC.exe ?>
<Root>
<!-- instructions for the 1st work center -->
<Location LocationID="10"
LaborHours="1.1"
MachineHours=".2" >Some text 1
<step>Manufacturing step 1 at this work center</step>
<step>Manufacturing step 2 at this work center</step>
</Location>
</Root>'
SELECT @myDoc
-- delete an attribute
SET @myDoc.modify('
delete /Root/Location/@MachineHours
')
SELECT @myDoc
-- delete an element
SET @myDoc.modify('
delete /Root/Location/step[2]
')
SELECT @myDoc
-- delete text node (in <Location>
SET @myDoc.modify('
delete /Root/Location/text()
')
SELECT @myDoc
-- delete all processing instructions
SET @myDoc.modify('
delete //processing-instruction()
')
SELECT @myDoc
B.Yazılmamış xml sütunda depolanan bir belgeden silme
Aşağıdaki örnekte, bir silmek xml dml deyimi kaldırır ikinci alt öğesi <Features> sütunda depolanan belge.
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 contents before delete
SELECT x.query(' //ProductDescription/Features')
FROM T
-- delete the second feature
UPDATE T
SET x.modify('delete /Root/ProductDescription/Features/*[2]')
-- verify the deletion
SELECT x.query(' //ProductDescription/Features')
FROM T
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 contents before delete
SELECT x.query(' //ProductDescription/Features')
FROM T
-- delete the second feature
UPDATE T
SET x.modify('delete /Root/ProductDescription/Features/*[2]')
-- verify the deletion
SELECT x.query(' //ProductDescription/Features')
FROM T
Önceki sorgudan aşağıdakilere dikkat edin:
Modify() yöntemi (xml veri türü) belirtmek için kullanılan silmek xml dml anahtar kelime.
Query() yöntemi (xml veri türü) belgesini sorgulamak için kullanılır.
C.Yazılı xml sütundan düğümleri silme
Bu örnek xml belgesinde depolanan üretim yönergeleri düğümleri siler bir yazılı xml sütun.
Örnekte, önce bir tablo (t) bir Daktilo ile oluşturduğunuz xml AdventureWorks veritabanındaki sütun. Sen o kopya bir üretim yönergeleri xml ProductModel tablosundaki yönergeleri sütun tablo t örneği ve bir veya daha fazla düğüm belgeden silin.
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
--1) insert <Location 1000/>. Note: <Root> must be singleton in the query
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" >
These are manu steps at location 1000.
<MI:step>New step1 instructions</MI:step>
Instructions for step 2 are here
<MI:step>New step 2 instructions</MI:step>
</MI:Location>
as first
into (/MI:root)[1]
')
go
select Instructions
from T
-- delete an attribute
update T
set Instructions.modify('
declare namespace MI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";
delete(/MI:root/MI:Location[@LocationID=1000]/@LaborHours)
')
go
select Instructions
from T
-- delete text in <location>
update T
set Instructions.modify('
declare namespace MI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";
delete(/MI:root/MI:Location[@LocationID=1000]/text())
')
go
select Instructions
from T
-- delete 2nd manu step at location 1000
update T
set Instructions.modify('
declare namespace MI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";
delete(/MI:root/MI:Location[@LocationID=1000]/MI:step[2])
')
go
select Instructions
from T
-- cleanup
drop table T
go
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
--1) insert <Location 1000/>. Note: <Root> must be singleton in the query
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" >
These are manu steps at location 1000.
<MI:step>New step1 instructions</MI:step>
Instructions for step 2 are here
<MI:step>New step 2 instructions</MI:step>
</MI:Location>
as first
into (/MI:root)[1]
')
go
select Instructions
from T
-- delete an attribute
update T
set Instructions.modify('
declare namespace MI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";
delete(/MI:root/MI:Location[@LocationID=1000]/@LaborHours)
')
go
select Instructions
from T
-- delete text in <location>
update T
set Instructions.modify('
declare namespace MI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";
delete(/MI:root/MI:Location[@LocationID=1000]/text())
')
go
select Instructions
from T
-- delete 2nd manu step at location 1000
update T
set Instructions.modify('
declare namespace MI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";
delete(/MI:root/MI:Location[@LocationID=1000]/MI:step[2])
')
go
select Instructions
from T
-- cleanup
drop table T
go
Ayrıca bkz.
Kavramlar
Yazılı xml yazılmamış xml karşılaştırın
xml veri değişikliği dili (xml dml)