2010-07-06 57 views
1

如果你有這樣的XML更換多個屬性與Transact-SQL替換 - XML

<AssetComponent name="AssetComponent4881" width="300"> 
    <Asset id="5" type="1" /> 
</AssetComponent> 
<AssetComponent name="AssetComponent4882" width="300"> 
    <Asset id="5" type="1" /> 
</AssetComponent> 

是否有可能取代所有的5至6的ID在一個查詢?

有了這個SQL只有一個屬性的時候可以更換:

SET @myDoc.modify(' 
    replace value of (//Asset/@id)[1] 
    with  sql:variable("@BinaryAssetId") 
') 

從SQL文檔,我可以看到的是,更換應該返回一個原子節點,只有一個,所以是有另一種方式做這個?

回答

1

上次我看到這個時,沒有這樣的方式 - replace一次只能操作一個物品。我當時提出的(不愉快的)解決方案是(僞代碼):

假設我們想要將記錄從一個狀態更改爲另一個狀態;讓我們稱這些狀態爲「從」狀態(例如具有Asset/@id = '5')和「到」狀態(例如具有Asset/@id = '6')。然後執行這個循環:

WHILE there exists a record that is in the 'from' state 
    BEGIN 
    Execute a .modify that changes the first record in the 'from' state 
     to being in the 'to' state 
    END 

這不是很漂亮,但它的工作原理。

+0

Thx,像一個魅力工作。 – 2010-07-06 14:24:41

1
While(@Maxcount <> 0) 
BEGIN 
     SET @myDoc.modify(' 
      replace value of (//Asset/@id)[sql:variable("@Maxcount")] 
      with  sql:variable("@BinaryAssetId") 
     ') 
     SET @MaxCount = @MaxCount - 1 
END