jjzjj

sql - 事务 SQL XQuery XML 查询

coder 2024-06-29 原文

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 中执行此操作,如果可能的话不使用循环等,例如简单的更新查询。

最佳答案

查询:

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

关于sql - 事务 SQL XQuery XML 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7682019/

有关sql - 事务 SQL XQuery XML 查询的更多相关文章

  1. ruby - ECONNRESET (Whois::ConnectionError) - 尝试在 Ruby 中查询 Whois 时出错 - 2

    我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.

  2. ruby - 我可以使用 aws-sdk-ruby 在 AWS S3 上使用事务性文件删除/上传吗? - 2

    我发现ActiveRecord::Base.transaction在复杂方法中非常有效。我想知道是否可以在如下事务中从AWSS3上传/删除文件:S3Object.transactiondo#writeintofiles#raiseanexceptionend引发异常后,每个操作都应在S3上回滚。S3Object这可能吗?? 最佳答案 虽然S3API具有批量删除功能,但它不支持事务,因为每个删除操作都可以独立于其他操作成功/失败。该API不提供任何批量上传功能(通过PUT或POST),因此每个上传操作都是通过一个独立的API调用完成的

  3. ruby-on-rails - 在 Rails 和 ActiveRecord 中查询时忽略某些字段 - 2

    我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr

  4. ruby - 分布式事务和队列,ruby,erlang,scala - 2

    我有一个涉及多台机器、消息队列和事务的问题。因此,例如用户点击网页,点击将消息发送到另一台机器,该机器将付款添加到用户的帐户。每秒可能有数千次点击。事务的所有方面都应该是容错的。我以前从未遇到过这样的事情,但一些阅读表明这是一个众所周知的问题。所以我的问题。我假设安全的方法是使用两阶段提交,但协议(protocol)是阻塞的,所以我不会获得所需的性能,我是否正确?我通常写Ruby,但似乎Redis之类的数据库和Rescue、RabbitMQ等消息队列系统对我的帮助不大——即使我实现某种两阶段提交,如果Redis崩溃,数据也会丢失,因为它本质上只是内存。所有这些让我开始关注erlang和

  5. Hive SQL 五大经典面试题 - 2

    目录第1题连续问题分析:解法:第2题分组问题分析:解法:第3题间隔连续问题分析:解法:第4题打折日期交叉问题分析:解法:第5题同时在线问题分析:解法:第1题连续问题如下数据为蚂蚁森林中用户领取的减少碳排放量iddtlowcarbon10012021-12-1212310022021-12-124510012021-12-134310012021-12-134510012021-12-132310022021-12-144510012021-12-1423010022021-12-154510012021-12-1523.......找出连续3天及以上减少碳排放量在100以上的用户分析:遇到这类

  6. sql - 查询忽略时间戳日期的时间范围 - 2

    我正在尝试查询我的Rails数据库(Postgres)中的购买表,我想查询时间范围。例如,我想知道在所有日期的下午2点到3点之间进行了多少次购买。此表中有一个created_at列,但我不知道如何在不搜索特定日期的情况下完成此操作。我试过:Purchases.where("created_atBETWEEN?and?",Time.now-1.hour,Time.now)但这最终只会搜索今天与那些时间的日期。 最佳答案 您需要使用PostgreSQL'sdate_part/extractfunction从created_at中提取小时

  7. ruby-on-rails - solr 清理查询 - 2

    我在Rails上使用带有ruby​​的solr。一切正常,我只需要知道是否有任何现有代码来清理用户输入,比如以?开头的查询。或* 最佳答案 我不知道执行此操作的任何代码,但理论上可以通过查看parsingcodeinLucene来完成并搜索thrownewParseException(只有16个匹配!)。在实践中,我认为您最好只捕获代码中的任何solr异常并显示“无效查询”消息或类似信息。编辑:这里有几个“sanitizer”:http://pivotallabs.com/users/zach/blog/articles/937-s

  8. ruby-on-rails - Rails 3 在一个查询中包含多个表 - 2

    我正在为锦标赛开发一个Rails应用程序。我在这个查询中使用了三个模型:classPlayertruehas_and_belongs_to_many:tournamentsclassTournament:destroyclassPlayerMatch"Player",:foreign_key=>"player_one"belongs_to:player_two,:class_name=>"Player",:foreign_key=>"player_two"在tournaments_controller的显示操作中,我调用以下查询:Tournament.where(:id=>params

  9. ruby-on-rails - Sunspot:如何对具有不同值的多个字段进行全文查询? - 2

    我想用sunspot重现以下原始solr查询q=exact_term_text:fooORterm_textv:foo*ORalternate_text:bar*但我无法通过标准的太阳黑子界面理解这是否可能以及如何实现,因为看起来:fulltext方法似乎不接受多个文本/搜索字段参数我不知道将什么参数作为第一个参数传递给fulltext,就好像我通过了"foo"或"bar"结果不匹配如果我传递一个空参数,我得到一个q=*:*范围过滤器(例如with(:term).starting_with('foo*')(顾名思义)作为过滤器查询应用,因此不参与评分。似乎可以手动编写字符串(或者可能使

  10. ruby - 如何使用 ruby​​ mysql2 执行事务 - 2

    我已经开始使用mysql2gem。我试图弄清楚一些基本的事情——其中之一是如何明确地执行事务(对于批处理操作,比如多个INSERT/UPDATE查询)。在旧的ruby-mysql中,这是我的方法:client=Mysql.real_connect(...)inserts=["INSERTINTO...","UPDATE..WHEREid=..",#etc]client.autocommit(false)inserts.eachdo|ins|beginclient.query(ins)rescue#handleerrorsorabortentirelyendendclient.commi

随机推荐