2008-12-18 4 views
5

Ich habe ein Problem mit dem Entfernen eines Attributs von einem Knoten.SQL Server xml.modify Methode löschen

Beispiel:

DECLARE @processID int 
SET @processID = 8 

DECLARE @xml XML 
SET @xml = 
'<Process id="10" name="Test 1"> 
    <Shapes> 
    <Shape id="1" name="Shape 1" subProcessID="8"> 
    </Shape> 
    <Shape id="2" name="Shape 2" subProcessID="9"> 
    </Shape> 
    </Shapes> 
    <Lines /> 
</Process>' 

SET @xml.modify('delete (/Process/Shapes/Shape/@subProcessID[/Process/Shapes/Shape/@subProcessID = sql:variable("@processID")])') 
SELECT @xml 

Gibt das Ergebnis:

<Process id="10" name="Test 1"> 
    <Shapes> 
    <Shape id="1" name="Shape 1" /> 
    <Shape id="2" name="Shape 2" /> 
    </Shapes> 
    <Lines /> 
</Process> 

Was würde Ich mag ist:

<Process id="10" name="Test 1"> 
    <Shapes> 
    <Shape id="1" name="Shape 1" /> 
    <Shape id="2" name="Shape 2" subProcessID="9" /> 
    </Shapes> 
    <Lines /> 
</Process> 

Was ist die Syntax, dies zu erreichen?

+1

fand ich die Lösung: SET @ xml.modify ('löschen (/ Prozess/Formen/Form [@subProcessID = sql: variable ("@ processID")]/@ subProcessID)') funktioniert der Trick für mich –

Antwort

0

Da die OP ist weg, aber er ließ mich die Lösung in einem Kommentar lassen, dass als Antwort hinzu:

DECLARE @processID int 
SET @processID = 8 

DECLARE @xml XML 
SET @xml = 
'<Process id="10" name="Test 1"> 
    <Shapes> 
    <Shape id="1" name="Shape 1" subProcessID="8"> 
    </Shape> 
    <Shape id="2" name="Shape 2" subProcessID="9"> 
    </Shape> 
    </Shapes> 
    <Lines /> 
</Process>' 

SET @xml.modify('delete (/Process/Shapes/Shape[@subProcessID = sql:variable("@processID")]/@subProcessID)') 
SELECT @xml 

Hier a working sqlfiddle dafür.

Verwandte Themen