XQuery 过滤器(如Where 子句)

发布于 2024-11-19 01:39:51 字数 1724 浏览 2 评论 0原文

我已根据特定条件从 XML 值中获取数据。 想法是拥有一张包含两列 ID 和 Data(XML 数据类型)的表。我必须获取特定 ID 的数据。

这是示例,我希望仅在西尔斯大厦的第一排获得结果。我得到两行。

IF OBJECT_ID('tempdb..#ExistExample') IS NOT NULL
DROP TABLE #ExistExample
GO

CREATE TABLE #ExistExample
(
  XMLID Int,
  XMLDocument xml
)

INSERT INTO #ExistExample
VALUES (100,'<Buildings>
  <Building>
    <Name>Sears Tower</Name>
    <Floor1>Yes</Floor1>
    <Floor2>Yes</Floor2>
    <Floor3>No</Floor3>
  </Building>
  <Building>
    <Name>IDS Building</Name>
      <Floor1>Yes</Floor1>
      <Floor2>Yes</Floor2>
      <Floor3>Yes</Floor3>
  </Building>
</Buildings>')

DECLARE @data varchar(1000)
DECLARE @ID INT
SET @ID = 101
SET @data = 'Sears Tower'

INSERT INTO #ExistExample
VALUES (101,'<Buildings>
  <Building>
    <Name>Sears Tower</Name>
    <Floor1>Yes</Floor1>
    <Floor2>Yes</Floor2>
    <Floor3>No</Floor3>
  </Building>
  <Building>
    <Name>IDS Building</Name>
      <Floor1>Yes</Floor1>
      <Floor2>Yes</Floor2>
      <Floor3>Yes</Floor3>
  </Building>
</Buildings>')

--SELECT * FROM #ExistExample

SELECT 
c.value('(Name/text())[1]','varchar(25)') AS BuildingName,
c.value('(Floor1/text())[1]','varchar(25)') AS Floor1,
c.value('(Floor2/text())[1]','varchar(25)') AS Floor2,
c.value('(Floor3/text())[1]','varchar(25)') AS Floor3
FROM #ExistExample
CROSS APPLY XMLDocument.nodes('/Buildings/Building') as t(c)
WHERE c.exist('//Building/Name[.=sql:variable("@data")]') = 1
AND XMLID = @ID

I have fetch the data based on specific condition from the XML value.
Idea is to have one table with two columns ID and Data(XML dataType). I have to fetch the data for specific ID.

Here is the example and I wish to achieve the result as only first row with Sears Tower ONLY. I am getting two rows.

IF OBJECT_ID('tempdb..#ExistExample') IS NOT NULL
DROP TABLE #ExistExample
GO

CREATE TABLE #ExistExample
(
  XMLID Int,
  XMLDocument xml
)

INSERT INTO #ExistExample
VALUES (100,'<Buildings>
  <Building>
    <Name>Sears Tower</Name>
    <Floor1>Yes</Floor1>
    <Floor2>Yes</Floor2>
    <Floor3>No</Floor3>
  </Building>
  <Building>
    <Name>IDS Building</Name>
      <Floor1>Yes</Floor1>
      <Floor2>Yes</Floor2>
      <Floor3>Yes</Floor3>
  </Building>
</Buildings>')

DECLARE @data varchar(1000)
DECLARE @ID INT
SET @ID = 101
SET @data = 'Sears Tower'

INSERT INTO #ExistExample
VALUES (101,'<Buildings>
  <Building>
    <Name>Sears Tower</Name>
    <Floor1>Yes</Floor1>
    <Floor2>Yes</Floor2>
    <Floor3>No</Floor3>
  </Building>
  <Building>
    <Name>IDS Building</Name>
      <Floor1>Yes</Floor1>
      <Floor2>Yes</Floor2>
      <Floor3>Yes</Floor3>
  </Building>
</Buildings>')

--SELECT * FROM #ExistExample

SELECT 
c.value('(Name/text())[1]','varchar(25)') AS BuildingName,
c.value('(Floor1/text())[1]','varchar(25)') AS Floor1,
c.value('(Floor2/text())[1]','varchar(25)') AS Floor2,
c.value('(Floor3/text())[1]','varchar(25)') AS Floor3
FROM #ExistExample
CROSS APPLY XMLDocument.nodes('/Buildings/Building') as t(c)
WHERE c.exist('//Building/Name[.=sql:variable("@data")]') = 1
AND XMLID = @ID

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

未蓝澄海的烟 2024-11-26 01:39:51

使用

DriverDetails/DriverDetail[ID eq 1]/*[not(self::ID)]

或者,如果您认为这更简单:

DriverDetails/DriverDetail[ID eq 1]/(PRN | Name))

这里假设(对于两个 XPath 表达式)表达式的计算结果是将 DriverDetails 的父节点作为初始上下文节点。

Use:

DriverDetails/DriverDetail[ID eq 1]/*[not(self::ID)]

or, if you consider this simpler:

DriverDetails/DriverDetail[ID eq 1]/(PRN | Name))

Here it is assumed that (for both XPath expressions) the expression is evaluated having as initial context node the parent of DriverDetails.

怀中猫帐中妖 2024-11-26 01:39:51

得到答案了。这应该是这样的:

SELECT c.value('(Name/text())[1]','varchar(25)') AS BuildingName, 
c.value('(Floor1/text())[1]','varchar(25)') AS Floor1, 
c.value('(Floor2/text())[1]','varchar(25)') AS Floor2, 
c.value('(Floor3/text())[1]','varchar(25)') AS Floor3
 FROM #ExistExample 
CROSS APPLY XMLDocument.nodes('/Buildings/Building') as t(c) 
WHERE c.value('(Name/text())[1]','varchar(25)') = @data 
AND XMLID = @ID

我不确定这是否是更好的方法,或者还有其他方法可以实现这一目标。

Got the answer. This should be like:

SELECT c.value('(Name/text())[1]','varchar(25)') AS BuildingName, 
c.value('(Floor1/text())[1]','varchar(25)') AS Floor1, 
c.value('(Floor2/text())[1]','varchar(25)') AS Floor2, 
c.value('(Floor3/text())[1]','varchar(25)') AS Floor3
 FROM #ExistExample 
CROSS APPLY XMLDocument.nodes('/Buildings/Building') as t(c) 
WHERE c.value('(Name/text())[1]','varchar(25)') = @data 
AND XMLID = @ID

I am not sure if this is the better way or there is any other way to achieve this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文