delete (XML DML)

Gilt für:SQL ServerAzure SQL-DatenbankAzure SQL Managed Instance

Löscht Knoten aus einer XML-Instanz.

Syntax

delete Expression  

Hinweis

Informationen zum Anzeigen der Transact-SQL-Syntax für SQL Server 2014 (12.x) und früher finden Sie unter Dokumentation zu früheren Versionen.

Argumente

Ausdruck
Ein XQuery-Ausdruck, der die zu löschenden Knoten identifiziert. Alle durch den Ausdruck ausgewählten Knoten sowie alle Knoten oder Werte, die darin enthalten sind, werden gelöscht. Wie unter insert (XML DML) beschrieben, muss dabei auf einen im Dokument vorhandenen Knoten verwiesen werden. Es darf sich nicht um einen konstruierten Knoten handeln. Der Ausdruck darf nicht der Stammknoten (/) sein. Wenn der Ausdruck eine leere Sequenz zurückgibt, wird nichts gelöscht, und es werden keine Fehler zurückgegeben.

Beispiele

A. Löschen von Knoten aus einem in einer nicht typisierten XML-Variablen gespeicherten Dokument

Das folgende Beispiel veranschaulicht das Löschen verschiedener Knoten aus einem Dokument. Zuerst wird einer Variablen vom Typ xml eine XML-Instanz zugewiesen. Anschließend löschen XML DML-Anweisungen verschiedene Knoten aus dem Dokument.

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. Löschen von Knoten aus einem in einer nicht typisierten XML-Spalte gespeicherten Dokument

Im folgenden Beispiel entfernt die XML DML-Anweisung für delete das zweite untergeordnete Element von <Features> aus dem in der Spalte gespeicherten Dokument.

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  

Beachten Sie hinsichtlich der vorherigen Abfrage Folgendes:

C. Löschen von Knoten aus einer typisierten XML-Spalte

Im folgenden Beispiel werden Knoten aus dem in einer typisierten xml -Spalte gespeicherten XML-Dokument mit den Fertigungsanweisungen gelöscht.

In diesem Beispiel erstellen Sie zunächst eine Tabelle (T) mit einer typisierten xml -Spalte in der AdventureWorks-Datenbank. Anschließend kopieren Sie eine XML-Instanz der Fertigungsanweisungen aus der Instructions-Spalte der ProductModel-Tabelle in Tabelle T und löschen einen oder mehrere Knoten aus dem Dokument.

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 

Weitere Informationen

Vergleichen von typisiertem XML mit nicht typisiertem XML
Erstellen von Instanzen der XML-Daten
xml Data Type Methods (xml-Datentypmethoden)
XML DML (Data Modification Language)