2013-03-15 70 views
1

我需要一些幫助 - 如何從非類型化的XML字段中刪除節點,其中的屬性等於一個sql變量?刪除節點,其中屬性=變量

這是我曾嘗試:

DECLARE @AttributeKey varchar(500) = 'TestColorBox1'; 

UPDATE 
    Numbers 
SET 
    AttributeKeyValues.modify('delete (/attributes/attribute[@key="{sql:variable("@AttributeKey")"]}/*') 
WHERE 
    AccountId = 2000046 

    SELECT * FROM Numbers 

PS。對不起,如果問題是模糊的,我從字面上開始用xquery昨天。我已經讀了您定義的SQL變量作爲

"sql:variable("@variableName")" 

錯誤:

Msg 9303, Level 16, State 1, Line 6 
    XQuery [Numbers.AttributeKeyValues.modify()]: Syntax error near '@', expected ']'. 

更新:

得到了它的無差錯運行:仍然不刪除我需要的節點刪除

DECLARE @AttributeKey varchar(500) = 'TestColorBox1'; 

UPDATE 
    Numbers 
SET 
    AttributeKeyValues.modify('delete (/attributes/attribute[@key="{sql:variable("@AttributeKey")}"])') 
WHERE 
    AccountId = 2000046 

    SELECT * FROM Numbers 

另一個更新:XML我們ED

<attributes> 
    <attribute key="TestColorBox1">TEST1</attribute> <!-- Targeted Field --> 
    <attribute key="test2345">TEST2</attribute> 
    <attribute key="test23454">TEST3</attribute> 
    <attribute key="test24568798">TEST4</attribute> 
    <attribute key="TEST123214124124">TEST5</attribute> 
</attributes> 

回答

1

刪除attribute節點在attributes其中屬性@key等於擊中當場局部變量@AttributeKey

modify('delete /attributes/attribute[@key = sql:variable("@AttributeKey")]') 
+0

的值。謝謝 – Eon 2013-03-15 13:57:43