2013-03-15 72 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