XQuery 过滤器(如Where 子句)
我已根据特定条件从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用:
或者,如果您认为这更简单:
这里假设(对于两个 XPath 表达式)表达式的计算结果是将
DriverDetails
的父节点作为初始上下文节点。Use:
or, if you consider this simpler:
Here it is assumed that (for both XPath expressions) the expression is evaluated having as initial context node the parent of
DriverDetails
.得到答案了。这应该是这样的:
我不确定这是否是更好的方法,或者还有其他方法可以实现这一目标。
Got the answer. This should be like:
I am not sure if this is the better way or there is any other way to achieve this.