2011-10-07 94 views
3
DECLARE @tProduct TABLE (
    [pProductId] [smallint] IDENTITY(1,1) PRIMARY KEY NOT NULl, 
    [ProductDetails] [xml] NOT NULL 
) 

    INSERT @tProduct 
    ([ProductDetails]) 
    VALUES 
    (N'<product> 
     <placeholder name="ProductHeader"><control name="pdRequiredMainHeading"><text position="placeholder1">Blah blah</text></control></placeholder> 
     <placeholder name="LeftColumn"> 
      <control name="pdRequiredSubHeading"><text position="placeholder1">Blah blah</text><text position="placeholder2">Blah blah</text></control> 
      <control name="pdRequiredParagraph"><text position="placeholder1">Blah blah</text></control> 
      <control name="pdOverlinedUnderlinedHeading"><text position="placeholder1">Blah blah</text></control> 
      <control name="pdParagraph"><text position="placeholder1">Blah blah</text></control> 
      </placeholder> 
      <placeholder name="RightColumn"> 
       <control name="pdRequiredMediumImage"><image position="placeholder1" alt="Blah blah">Blahblah.gif</image></control> 
       <control name="pdMediumImage"><image position="placeholder1" alt="">BlahBlah2.gif</image></control> 
       <control name="pdRoundedBorderHeadingUnorderedList"><text position="placeholder1">Blah blah</text><ul position="placeholder2"><li>Blah blah</li></ul></control> 
       <control name="pdMediumImage"><image position="placeholder1" alt="">The-Image-I-Want-1.gif</image></control> 
      </placeholder> 
     </product>') 

    INSERT @tProduct 
    ([ProductDetails]) 
    VALUES 
    (N'<product> 
     <placeholder name="ProductHeader"><control name="pdRequiredMainHeading"><text position="placeholder1">Blah blah</text></control></placeholder> 
     <placeholder name="LeftColumn"> 
      <control name="pdRequiredSubHeading"><text position="placeholder1">Blah blah</text><text position="placeholder2">Blah blah</text></control> 
      <control name="pdRequiredParagraph"><text position="placeholder1">Blah blah</text></control> 
      <control name="pdOverlinedUnderlinedHeading"><text position="placeholder1">Blah blah</text></control> 
      <control name="pdParagraph"><text position="placeholder1">Blah blah</text></control> 
     </placeholder> 
     <placeholder name="RightColumn"> 
      <control name="pdRequiredMediumImage"><image position="placeholder1" alt="Blah blah">Blahblah.gif</image></control> 
      <control name="pdRoundedBorderHeading"><text position="placeholder1">Blah blah</text><ul position="placeholder2"><li>Blah blah</li></ul></control> 
      <control name="pdMediumImage"><image position="placeholder1" alt="">The-Image-I-Want-12.gif</image></control> 
     </placeholder> 
     </product>') 

    INSERT @tProduct 
    ([ProductDetails]) 
    VALUES 
    (N'<product> 
     <placeholder name="ProductHeader"><control name="pdRequiredMainHeading"><text position="placeholder1">Blah blah</text></control></placeholder> 
     <placeholder name="LeftColumn"> 
      <control name="pdRequiredSubHeading"><text position="placeholder1">Blah blah</text><text position="placeholder2">Blah blah</text></control> 
      <control name="pdRequiredParagraph"><text position="placeholder1">Blah blah</text></control> 
      <control name="pdOverlinedUnderlinedHeading"><text position="placeholder1">Blah blah</text></control> 
      <control name="pdParagraph"><text position="placeholder1">Blah blah</text></control> 
     </placeholder> 
     <placeholder name="RightColumn"> 
      <control name="pdRequiredMediumImage"><image position="placeholder1" alt="">The-Image-I-Want-1.gif123.gif</image></control> 
      <control name="pdRoundedBorderHeadingUnorderedList"><text position="placeholder1">Blah blah</text><ul position="placeholder2"><li>Blah blah</li></ul></control> 
      <control name="pdMediumImage"><image position="placeholder1" alt="Blah blah">Blahblah.gif</image></control> 
     </placeholder> 
     </product>') 

    INSERT @tProduct 
    ([ProductDetails]) 
    VALUES 
    (N'<product> 
     <placeholder name="ProductHeader"><control name="pdRequiredMainHeading"><text position="placeholder1">Blah blah</text></control></placeholder> 
     <placeholder name="LeftColumn"> 
      <control name="pdRequiredSubHeading"><text position="placeholder1">Blah blah</text><text position="placeholder2">Blah blah</text></control> 
      <control name="pdRequiredParagraph"><text position="placeholder1">Blah blah</text></control> 
      <control name="pdOverlinedUnderlinedHeading"><text position="placeholder1">Blah blah</text></control> 
      <control name="pdParagraph"><text position="placeholder1">Blah blah</text></control> 
     </placeholder> 
     <placeholder name="RightColumn"> 
      <control name="pdRequiredMediumImage"><image position="placeholder1" alt="">The-Image-I-Want-1.gif1234.gif</image></control> 
      <control name="pdRoundedBorder"><text position="placeholder1">Blah blah</text><ul position="placeholder2"><li>Blah blah</li></ul></control> 
      <control name="pdMediumImage"><image position="placeholder1" alt="Blah blah">Blahblah.gif</image></control> 
     </placeholder> 
     </product>') 

    -- ITS AN UPDATE I WANT BUT EVEN THIS I CANT GET TO WORK AS IT DOESNT BRING BACK EVERY IMAGE 

    SELECT pProductId, ProductDetails 
    FROM @tProduct 
    WHERE (ProductDetails.nodes('(//product/placeholder/control/image)') LIKE 'The-Image-I-Want-%') 

我試過了包括WHERE ProductDetails.value在內的各種版本,但是我又可以帶回一些節點但不是全部。Transact SQL XQuery XML查詢

我試着創建一個更新查詢,從Image-I-Want-.gif中刪除數字。

Image-I-Want-1.gif becomes Image-I-Want-.gif 

Image-I-Want-12.gif becomes Image-I-Want-.gif 

Image-I-Want-123.gif becomes Image-I-Want-.gif 

等等等等

但我甚至不能讓它選擇所有需要的圖像更不用說對其進行更新。它的xQuery語法不能完全正確,我也不能找到一個很好的示例,因爲我嘗試的所有內容都出現了錯誤。

我可以在c#或一些等價的腳本,但我真的很想知道如何在Transact-Sql中使用xQuery而不使用循環,如果可能的話,就像一個簡單的更新查詢。

回答

5

查詢:

select pProductId, ProductDetails 
from @tProduct 
where ProductDetails.exist('/product/placeholder/control/image[contains(., "The-Image-I-Want-")]') = 1 

更新:

update @tProduct 
set ProductDetails.modify('replace value of (/product/placeholder/control/image[contains(., "The-Image-I-Want-")]/text())[1] with "The-Image-I-Want-.gif"') 
where ProductDetails.exist('/product/placeholder/control/image[contains(., "The-Image-I-Want-")]') = 1 
+0

非常好,謝謝你,是對現貨:-) –