jjzjj

sql - XML SQL Server 查询以检索属性值和标记文本值

coder 2024-07-05 原文

我是在 SQL Server 中查询 XML 的新手,这不是我的日常任务之一,我正在帮助一位同事。例如,我将以下 XML 存储在 SQL 的列中:

我如何检索两列,一列具有 id 值,另一列具有来自下一个内部标记的值(例如:2109 和 0 来自 <Bool>0</Bool>)

期望的输出:

id  value
2109    0
341     2
342     10
2196    753064REPJ1
1283    2
1293    0_Imprumutat

谢谢!

<Item id="645" flags="769">
  <Row>
    <Item id="2109" flags="257">
      <Bool>0</Bool>
    </Item>
    <Item id="341" flags="257">
      <Str>2</Str>
    </Item>
    <Item id="342" flags="257">
      <Str>10</Str>
    </Item>
    <Item id="2196" flags="257">
      <Str>753064REPJ1</Str>
    </Item>
    <Item id="1283" flags="257">
      <Row>
        <Str>2</Str>
      </Row>
    </Item>
    <Item id="1293" flags="257">
      <Row>
        <Str>0_Imprumutat</Str>
      </Row>
    </Item>
    <Item id="1251" flags="257">
      <Str>2079759</Str>
    </Item>
    <Item id="2101" flags="257">
      <Dbl>500000</Dbl>
    </Item>
    <Item id="343" flags="257">
      <Str>2</Str>
    </Item>
    <Item id="2065" flags="257">
      <Dbl>1000000</Dbl>
    </Item>
    <Item id="2098" flags="257">
      <Dbl>0</Dbl>
    </Item>
    <Item id="2102" flags="257">
      <Dbl>0</Dbl>
    </Item>
    <Item id="2100" flags="257">
      <Dbl>0</Dbl>
    </Item>
    <Item id="2099" flags="257">
      <Dbl>0</Dbl>
    </Item>
    <Item id="359" flags="513">
      <Int>1000000</Int>
    </Item>
    <Item id="361" flags="513">
      <Int>1000000</Int>
    </Item>
    <Item id="347" flags="513">
      <Int>1000000</Int>
    </Item>
    <Item id="2346" flags="513">
      <Int>1000000</Int>
    </Item>
    <Item id="1912" flags="513">
      <Str>1234</Str>
    </Item>
    <Item id="2219" flags="513">
      <Str>1234</Str>
    </Item>
    <Item id="356" flags="513">
      <Int>1999</Int>
    </Item>
    <Item id="2218" flags="513">
      <Str>123</Str>
    </Item>
    <Item id="2220" flags="513">
      <Str>123</Str>
    </Item>
    <Item id="2702" flags="513">
      <Date>2016-10-02Z</Date>
    </Item>
    <Item id="377" flags="513">
      <Int>1</Int>
    </Item>
    <Item id="1218" flags="513">
      <Int>0</Int>
    </Item>
    <Item id="1219" flags="513">
      <Int>0</Int>
    </Item>
    <Item id="1596" flags="513">
      <Int>2</Int>
    </Item>
    <Item id="351" flags="513">
      <Str>Adresa</Str>
    </Item>
    <Item id="352" flags="513">
      <Str>B3</Str>
    </Item>
    <Item id="353" flags="513">
      <Str>LOc</Str>
    </Item>
    <Item id="354" flags="513">
      <Int>1</Int>
    </Item>
    <Item id="355" flags="513">
      <Str>jfdkls</Str>
    </Item>
    <Item id="1288" flags="513">
      <Int>3</Int>
    </Item>
    <Item id="368" flags="513">
      <Int>9</Int>
    </Item>
    <Item id="1887" flags="513">
      <Str>Realtech Services SRL</Str>
    </Item>
    <Item id="370" flags="513">
      <Date>2015-10-02Z</Date>
    </Item>
    <Item id="375" flags="513">
      <Int>2</Int>
    </Item>
    <Item id="371" flags="513">
      <Str>Asirom</Str>
    </Item>
    <Item id="372" flags="513">
      <Int>1000000</Int>
    </Item>
    <Item id="373" flags="513">
      <Str>RON</Str>
    </Item>
    <Item id="374" flags="513">
      <Date>2016-01-05Z</Date>
    </Item>
  </Row>
  <Row>
    <Item id="2109" flags="257">
      <Bool>0</Bool>
    </Item>
    <Item id="341" flags="257">
      <Str>1</Str>
    </Item>
    <Item id="342" flags="257">
      <Str>1</Str>
    </Item>
    <Item id="2196" flags="257">
      <Str>753064REPF2</Str>
    </Item>
    <Item id="1283" flags="257">
      <Row>
        <Str>2</Str>
      </Row>
    </Item>
    <Item id="1293" flags="257">
      <Row>
        <Str>0_Imprumutat</Str>
      </Row>
    </Item>
    <Item id="1251" flags="257">
      <Str>2079759</Str>
    </Item>
    <Item id="2101" flags="257">
      <Dbl>500000</Dbl>
    </Item>
    <Item id="343" flags="257">
      <Str>1</Str>
    </Item>
    <Item id="2065" flags="257">
      <Dbl>1000000</Dbl>
    </Item>
    <Item id="2098" flags="257">
      <Dbl>0</Dbl>
    </Item>
    <Item id="2102" flags="257">
      <Dbl>0</Dbl>
    </Item>
    <Item id="2100" flags="257">
      <Dbl>0</Dbl>
    </Item>
    <Item id="2099" flags="257">
      <Dbl>0</Dbl>
    </Item>
    <Item id="347" flags="513">
      <Int>1000000</Int>
    </Item>
    <Item id="2346" flags="513">
      <Int>1000000</Int>
    </Item>
    <Item id="1912" flags="513">
      <Str>123</Str>
    </Item>
    <Item id="2219" flags="513">
      <Str>1236</Str>
    </Item>
    <Item id="356" flags="513">
      <Int>1999</Int>
    </Item>
    <Item id="2218" flags="513">
      <Str>123</Str>
    </Item>
    <Item id="2702" flags="513">
      <Date>2016-01-02Z</Date>
    </Item>
    <Item id="377" flags="513">
      <Int>1</Int>
    </Item>
    <Item id="1218" flags="513">
      <Int>0</Int>
    </Item>
    <Item id="1219" flags="513">
      <Int>0</Int>
    </Item>
    <Item id="1596" flags="513">
      <Int>2</Int>
    </Item>
    <Item id="351" flags="513">
      <Str>jgfkdl</Str>
    </Item>
    <Item id="352" flags="513">
      <Str>B3</Str>
    </Item>
    <Item id="353" flags="513">
      <Str>gfdgfd</Str>
    </Item>
    <Item id="354" flags="513">
      <Int>1</Int>
    </Item>
    <Item id="355" flags="513">
      <Str>gfdgfd</Str>
    </Item>
    <Item id="357" flags="513">
      <Int>9</Int>
    </Item>
    <Item id="1288" flags="513">
      <Int>3</Int>
    </Item>
    <Item id="368" flags="513">
      <Int>9</Int>
    </Item>
    <Item id="1887" flags="513">
      <Str>Euroeval SRL</Str>
    </Item>
    <Item id="370" flags="513">
      <Date>2015-09-02Z</Date>
    </Item>
    <Item id="375" flags="513">
      <Int>2</Int>
    </Item>
    <Item id="371" flags="513">
      <Str>Allianz-Tiriac</Str>
    </Item>
    <Item id="372" flags="513">
      <Int>1000000</Int>
    </Item>
    <Item id="373" flags="513">
      <Str>RON</Str>
    </Item>
    <Item id="374" flags="513">
      <Date>2017-10-28Z</Date>
    </Item>
  </Row>
  <Row>
    <Item id="2109" flags="257">
      <Bool>0</Bool>
    </Item>
    <Item id="341" flags="257">
      <Str>1</Str>
    </Item>
    <Item id="342" flags="769">
      <Str>14</Str>
    </Item>
    <Item id="2196" flags="257">
      <Str>753064REPF3</Str>
    </Item>
    <Item id="1283" flags="257">
      <Row>
        <Str>2</Str>
      </Row>
    </Item>
    <Item id="1293" flags="257">
      <Row>
        <Str>0_Imprumutat</Str>
      </Row>
    </Item>
    <Item id="1251" flags="257">
      <Str>2079759</Str>
    </Item>
    <Item id="2101" flags="257">
      <Dbl>500000</Dbl>
    </Item>
    <Item id="343" flags="257">
      <Str>1</Str>
    </Item>
    <Item id="2065" flags="257">
      <Dbl>1000000</Dbl>
    </Item>
    <Item id="2100" flags="257">
      <Dbl>0</Dbl>
    </Item>
    <Item id="2099" flags="257">
      <Dbl>0</Dbl>
    </Item>
    <Item id="2102" flags="257">
      <Dbl>0</Dbl>
    </Item>
    <Item id="2098" flags="257">
      <Dbl>0</Dbl>
    </Item>
    <Item id="359" flags="513">
      <Int>1000000</Int>
    </Item>
    <Item id="361" flags="513">
      <Int>1000000</Int>
    </Item>
    <Item id="347" flags="513">
      <Int>1000000</Int>
    </Item>
    <Item id="2346" flags="513">
      <Int>1000000</Int>
    </Item>
    <Item id="1912" flags="513">
      <Str>12346</Str>
    </Item>
    <Item id="2219" flags="513">
      <Str>54</Str>
    </Item>
    <Item id="356" flags="513">
      <Int>1999</Int>
    </Item>
    <Item id="2218" flags="513">
      <Str>123</Str>
    </Item>
    <Item id="2220" flags="513">
      <Str>123</Str>
    </Item>
    <Item id="2702" flags="513">
      <Date>2016-10-02Z</Date>
    </Item>
    <Item id="377" flags="513">
      <Int>1</Int>
    </Item>
    <Item id="1218" flags="513">
      <Int>0</Int>
    </Item>
    <Item id="1219" flags="513">
      <Int>0</Int>
    </Item>
    <Item id="1596" flags="513">
      <Int>2</Int>
    </Item>
    <Item id="351" flags="513">
      <Str>iuyiy</Str>
    </Item>
    <Item id="352" flags="513">
      <Str>B2</Str>
    </Item>
    <Item id="353" flags="513">
      <Str>kjhlhj</Str>
    </Item>
    <Item id="354" flags="513">
      <Int>1</Int>
    </Item>
    <Item id="355" flags="513">
      <Str>lkjhljh</Str>
    </Item>
    <Item id="357" flags="513">
      <Int>9</Int>
    </Item>
    <Item id="1288" flags="513">
      <Int>1</Int>
    </Item>
    <Item id="368" flags="513">
      <Int>9</Int>
    </Item>
    <Item id="1887" flags="513">
      <Str>Euroeval SRL</Str>
    </Item>
    <Item id="370" flags="513">
      <Date>2016-06-02Z</Date>
    </Item>
    <Item id="375" flags="513">
      <Int>2</Int>
    </Item>
    <Item id="371" flags="513">
      <Str>Uniqa</Str>
    </Item>
    <Item id="372" flags="513">
      <Int>1000000</Int>
    </Item>
    <Item id="373" flags="513">
      <Str>RON</Str>
    </Item>
    <Item id="374" flags="513">
      <Date>2016-02-21Z</Date>
    </Item>
  </Row>
</Item>

最佳答案

您需要使用 XQuery,外部应用 并提供目标节点的路径。

最终查询

SELECT  n.a.value('@id','int') as id,n.a.value('(Bool)[1]','bit')
from xmldata x
        outer apply x.data.nodes('Item/Row/Item')as n(a)
where n.a.value('(Bool)[1]','varchar(max)') is not null

解释:

导航到所需的节点后,使用 n.a.value('@Attribute','datatype') 语法获取所需的属性,n.a.value('(ElementName)[1]', 'datatype') 获取元素值的语法。

关于sql - XML SQL Server 查询以检索属性值和标记文本值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32882430/

有关sql - XML SQL Server 查询以检索属性值和标记文本值的更多相关文章

  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 - 使用 ruby​​ 将 HTML 转换为纯文本并维护结构/格式 - 2

    我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h

  3. ruby-on-rails - 如果为空或不验证数值,则使属性默认为 0 - 2

    我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val

  4. ruby-on-rails - 在混合/模块中覆盖模型的属性访问器 - 2

    我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah

  5. ruby - 多个属性的 update_column 方法 - 2

    我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2

  6. ruby - Nokogiri 剥离所有属性 - 2

    我有这个html标记:我想得到这个:我如何使用Nokogiri做到这一点? 最佳答案 require'nokogiri'doc=Nokogiri::HTML('')您可以通过xpath删除所有属性:doc.xpath('//@*').remove或者,如果您需要做一些更复杂的事情,有时使用以下方法遍历所有元素会更容易:doc.traversedo|node|node.keys.eachdo|attribute|node.deleteattributeendend 关于ruby-Nokog

  7. 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

  8. ruby-on-rails - Rails 模型——非持久类成员或属性? - 2

    对于Rails模型,是否可以/建议让一个类的成员不持久保存到数据库中?我想将用户最后选择的类型存储在session变量中。由于我无法从我的模型中设置session变量,我想将值存储在一个“虚拟”类成员中,该成员只是将值传递回Controller。你能有这样的类(class)成员吗? 最佳答案 将非持久属性添加到Rails模型就像任何其他Ruby类一样:classUser扩展解释:在Ruby中,所有实例变量都是私有(private)的,不需要在赋值前定义。attr_accessor创建一个setter和getter方法:classUs

  9. 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以上的用户分析:遇到这类

  10. 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中提取小时

随机推荐