[英]Xpath Get data from xml column with out cross apply
I have a table in SQL Server 2012 with this structure:我在 SQL Server 2012 中有一个具有以下结构的表:
USE tempdb;
GO
-- DDL and sample data population, start
DROP TABLE IF EXISTS [dbo].[tblStepList];
CREATE TABLE [dbo].[tblStepList](
[listid] [int] ,
[Data] [xml] NOT NULL
);
INSERT INTO dbo.tblStepList (Listid, [Data]) VALUES
(1, N'<Steplist>
<Step>
<StepId>e36a3450-1c8f-44da-b4d0-58e5bfe2a987</StepId>
<Rank>1</Rank>
<IsComplete>false</IsComplete>
<TextReadingId>10</TextReadingId>
</Step>
<Step>
<StepId>4078c1b1-71ea-4578-ba61-d2f6a5126ba1</StepId>
<Rank>2</Rank>
<TextReadingName>reading1</TextReadingName>
<TextReadingId>12</TextReadingId>
</Step>
</Steplist>'),
(2, N'<Steplist>
<Step>
<StepId>d9e42387-56e3-40a1-9698-e89c930d98d1</StepId>
<Rank>1</Rank>
<IsComplete>false</IsComplete>
<TextReadingName>bug-8588_Updated3</TextReadingName>
<TextReadingId>0</TextReadingId>
</Step>
<Step>
<StepId>e5eaf947-24e1-4d3b-a92a-d6a90841293b</StepId>
<Rank>2</Rank>
<TextReadingId>1</TextReadingId>
</Step>
</Steplist>'),
(3, N'<Steplist>
<Step>
<StepId>d9e42387-56e3-40a1-9698-58e5bfe2a987</StepId>
<Rank>1</Rank>
<IsComplete>true</IsComplete>
<TextReadingName>bug-8588_Updated3</TextReadingName>
<TextReadingId>1</TextReadingId>
</Step>
<Step>
<StepId>e5eaf947-24e1-4d3b-a92a-d2f6a5126ba1</StepId>
<Rank>2</Rank>
<TextReadingId>1</TextReadingId>
</Step>
</Steplist>');
-- DDL and sample data population, end
I want sql query that can give me the rows of listids where textreadingid = 1 exists in the data xml.我想要 sql 查询,它可以给我数据 xml 中 textreadingid = 1 的 listid 行。
Which means in this case my result will be a table with ListIds 2, 3这意味着在这种情况下,我的结果将是一个 ListIds 2、3 的表
What I tried below works but uses cross apply which was super slow in large data.我在下面尝试的方法有效,但使用交叉应用,这在大数据中非常慢。 I want a soltion with out cross apply.
我想要一个没有交叉应用的解决方案。
select distinct Listid from
(
SELECT
s.Listid,
x.XmlCol.value('(TextReadingId)[1]', 'int') as [TextReadingId]
FROM tblStepList s
CROSS APPLY s.Data.nodes('/Steplist/Step') x(XmlCol)
) a
where TextReadingId = 1
The slow bit is not the CROSS APPLY.nodes
but the DISTINCT
.慢位不是
CROSS APPLY.nodes
而是DISTINCT
。
Be that as it may, you can do this using the .exist
function尽管如此,您可以使用
.exist
执行此操作
select
Listid
FROM tblStepList s
WHERE s.Data.exist('/Steplist/Step/TextReadingId[text()="1"]') = 1
I'm not familiar with sql
and xquery
you are using here, but the XPath expression to select Steplists
you are looking for could be something like the following:我不熟悉您在这里使用的
sql
和xquery
,但是您正在寻找的 XPath 表达式到 select Steplists
可能类似于以下内容:
"//Steplist[.//TextReadingId[text()='1']]"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.