oracle 在 SQL 中搜索 XML 列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/697256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 17:59:59  来源:igfitidea点击:

Search XML Column in SQL

xmloracleselect

提问by enormous81

? have an XML document to store against records in an Oracle Database.

? 有一个 XML 文档来存储 Oracle 数据库中的记录。

The table CourseXML will contain:

表 CourseXML 将包含:

Record_Number  int
XML_Type       int
XMLDoc         clob
...etc

? would like to make a search in XMLDoc column by XML tags. The XML document has an XML Schema like this:

? 想通过 XML 标签在 XMLDoc 列中进行搜索。XML 文档有一个像这样的 XML 架构:

<root>
  <UnitID="2" Name="Jerry" SName="Potter"/>
  <UnitID="3" Name="Jim" SName="Carelyn"/>
</root>

? want to make search in UnitID="2" and i only want Jerry's xml row. How I have to make a select statement query to get that xml row?

? 想在 UnitID="2" 中进行搜索,我只想要 Jerry 的 xml 行。我必须如何进行选择语句查询才能获取该 xml 行?

回答by gbn

You may have to play with the nodes bit to get it exact.

您可能必须使用节点位才能使其准确。

SELECT
    y.item.value('@UnitID', 'int') AS UnitID,
    y.item.value('@Name', 'varchar(100)') AS [Name],
    y.item.value('@SName', 'varchar(100)') AS [SName]
FROM
    <table>
    CROSS APPLY
    XMLDoc.nodes('/root') AS y(item)
WHERE
    y.item.value('@UnitID', 'int') = 2

Edit: corrected code to use table, not xml local variable

编辑:更正代码以使用表,而不是 xml 局部变量

回答by marc_s

You have plenty of ways of getting it. "gbn" showed one way - two other are here.

你有很多方法可以得到它。“gbn”显示了一种方式——另外两种在这里。

If you want the whole "row" (I assumed you'll put these things into a tag), try this:

如果你想要整个“行”(我假设你会把这些东西放到一个标签中),试试这个:

select
    xmldoc.query('//node[@UnitID="2"]')
from
    xmltest

If you want just the "Name" attribute from the tag, use this:

如果您只想要标签中的“名称”属性,请使用以下命令:

select
    xmldoc.value('(//node[@UnitID="2"]/@Name)[1]', 'varchar(20)')
from
    xmltest

If you need to access a whole bunch of attributes and/or subelements, use gbn's approach with the "CROSS APPLY xmldoc.nodes(....)".

如果您需要访问一大堆属性和/或子元素,请使用 gbn 的“CROSS APPLY xmldoc.nodes(....)”方法。

Enjoy! XML Support in SQL Server 2005 is really quite extensive and useful!

享受!SQL Server 2005 中的 XML 支持确实非常广泛和有用!

Marc

马克

回答by Jaime Bula

Being the structure, and using Full Text Search.

作为结构,并使用全文搜索。

<Root>
  <Tags>
    <TagName>Actividad</TagName>
    <Valor>Actividad 2</Valor>
  </Tags>
  <Tags>
    <TagName>Cliente</TagName>
    <Valor>Alpina</Valor>
  </Tags>
</Root>
 select 
     Filename
 from 
 Files
 where 
 CONTAINS(Tags,'Actividad')  and
 CONTAINS(Tags,'Cliente')  and
 Tags.exist('//Tags/Valor/text()[contains(., "Actividad 1")]')  = 1 and
 Tags.exist('//Tags/Valor/text()[contains(., "ADV")]')  = 1

I do not recommend using attributes on the XML, because full text search cannot be performed on attributes (no matter what the SQL 2008 R2 Docs say).

我不建议在 XML 上使用属性,因为不能对属性执行全文搜索(无论 SQL 2008 R2 Docs 怎么说)。

Please Refer to this

请参考这个

回答by Matt Grande

SELECT * FROM CourseXML
WHERE XMLDoc = 'UnitID="2"'

Isn't that it? Or am I misunderstanding something?

不就是这样吗?还是我误解了什么?