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,但我又可以带回一些节点但不是全部。
我试着创建一个更新查询,从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#或者某些等价脚本编写脚本,但我真的很想知道如何使用xQuery在Transact-Sql中执行它而不使用循环,如果可能的话,就像简单的更新查询一样。
答案 0 :(得分: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