hapus (XML DML)

Berlaku untuk:SQL ServerAzure SQL DatabaseAzure SQL Managed Instance

Menghapus simpul dari instans XML.

Sintaksis

delete Expression  

Catatan

Untuk melihat sintaks Transact-SQL untuk SQL Server 2014 (12.x) dan versi yang lebih lama, lihat Dokumentasi versi sebelumnya.

Argumen

Expression
Adalah ekspresi XQuery yang mengidentifikasi simpul yang akan dihapus. Semua simpul yang dipilih oleh ekspresi, dan juga semua simpul atau nilai yang terkandung dalam simpul yang dipilih, akan dihapus. Seperti yang dijelaskan dalam sisipkan (XML DML), ini harus menjadi referensi ke simpul yang ada dalam dokumen. Ini tidak bisa menjadi simpul yang dibangun. Ekspresi tidak boleh menjadi simpul root (/). Jika ekspresi mengembalikan urutan kosong, tidak ada penghapusan yang terjadi dan tidak ada kesalahan yang dikembalikan.

Contoh

J. Menghapus simpul dari dokumen yang disimpan dalam variabel xml yang tidak dititip

Contoh berikut mengilustrasikan cara menghapus berbagai simpul dari dokumen. Pertama, instans XML ditetapkan ke variabel jenis xml . Kemudian, hapus pernyataan DML XML berikutnya menghapus berbagai simpul dari dokumen.

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. Menghapus simpul dari dokumen yang disimpan dalam kolom xml yang tidak dititip

Dalam contoh berikut, pernyataan hapus XML DML menghapus elemen turunan <Features> kedua dari dokumen yang disimpan di kolom.

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  

Perhatikan hal berikut ini dari kueri sebelumnya:

C. Menghapus simpul dari kolom xml yang ditik

Contoh ini menghapus simpul dari dokumen XML instruksi manufaktur yang disimpan dalam kolom xml yang ditik.

Dalam contoh, Anda terlebih dahulu membuat tabel (T) dengan kolom xml yang ditik di database AdventureWorks. Anda kemudian menyalin instans XML instruksi manufaktur dari kolom Instruksi dalam tabel ProductModel ke dalam tabel T dan menghapus satu atau beberapa simpul dari dokumen.

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  
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  
SET 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 

Lihat Juga

Membandingkan XML Yang Dititik dengan XML Yang Tidak Dititik
Membuat Instans Data XML
Metode Tipe Data xml
Bahasa Modifikasi Data XML (XML DML)