XQuery sql 仅在存在时选择节点
Posted
技术标签:
【中文标题】XQuery sql 仅在存在时选择节点【英文标题】:XQuery sql select node only if exists 【发布时间】:2021-12-18 22:40:47 【问题描述】:我正在使用 SQL Server 2012 我正在寻找一种方法来从我的表中选择一些具有 XML 数据类型的节点值,如下所示。
我有以下架构
USE tempdb;
GO
DROP TABLE IF EXISTS [dbo].[tblStepList];
CREATE TABLE [dbo].[tblStepList](
[ToDoId] [int] IDENTITY(1,1) NOT NULL,
[Data] [xml] NOT NULL
);
INSERT INTO dbo.tblStepList ([Data]) VALUES
(N'<Steplist>
<Step>
<StepId>e36a3450-1c8f-44da-b4d0-58e5bfe2a987</StepId>
<Rank>1</Rank>
<IsComplete>false</IsComplete>
<TextReadingName>bug-8588_Updated3</TextReadingName>
</Step>
<Step>
<StepId>4078c1b1-71ea-4578-ba61-d2f6a5126ba1</StepId>
<Rank>2</Rank>
<TextReadingName>reading1</TextReadingName>
<TextReadingId>12</TextReadingId>
</Step>
</Steplist>');
INSERT INTO dbo.tblStepList ([Data]) VALUES
(N'<Steplist>
<Step>
<StepId>9960222B-897F-44E9-82FE-F33705D0C2B6</StepId>
<Rank>1</Rank>
<IsComplete>false</IsComplete>
<TextReadingName>bug-8588_Updated3</TextReadingName>
<TextReadingId>0</TextReadingId>
</Step>
<Step>
<StepId>11D70A50-08AC-4767-A0D3-87717384FF45</StepId>
<Rank>2</Rank>
<TextReadingName>reading1</TextReadingName>
</Step>
</Steplist>')
现在我想要一个 select 语句来获取没有 TextReadingId 节点的所有步骤,这是我尝试过的,但我无法做到存在于节点中
SELECT
x.XmlCol.value('(StepId)[1]', 'uniqueidentifier') as StepId
FROM tblStepList s
CROSS APPLY s.Data.nodes('/Steplist/Step') x(XmlCol) --where not exists('/Steplist/Step/TextReadingId')
所以我的预期输出是所有没有 TextreadingID 的节点 StepIds
Step:
E36A3450-1C8F-44DA-B4D0-58E5BFE2A987
11D70A50-08AC-4767-A0D3-87717384FF45
【问题讨论】:
【参考方案1】:请尝试以下解决方案。
XPath 谓词 .nodes('/Steplist/Step[not(TextReadingId)]')
可以发挥所有作用。
SQL
USE tempdb;
GO
-- DDL and sample data population, start
DROP TABLE IF EXISTS [dbo].[tblStepList];
CREATE TABLE [dbo].[tblStepList](
[ToDoId] [int] IDENTITY(1,1) NOT NULL,
[Data] [xml] NOT NULL
);
INSERT INTO dbo.tblStepList ([Data]) VALUES
(N'<Steplist>
<Step>
<StepId>e36a3450-1c8f-44da-b4d0-58e5bfe2a987</StepId>
<Rank>1</Rank>
<IsComplete>false</IsComplete>
<TextReadingName>bug-8588_Updated3</TextReadingName>
</Step>
<Step>
<StepId>4078c1b1-71ea-4578-ba61-d2f6a5126ba1</StepId>
<Rank>2</Rank>
<TextReadingName>reading1</TextReadingName>
<TextReadingId>12</TextReadingId>
</Step>
</Steplist>'),
(N'<Steplist>
<Step>
<StepId>9960222B-897F-44E9-82FE-F33705D0C2B6</StepId>
<Rank>1</Rank>
<IsComplete>false</IsComplete>
<TextReadingName>bug-8588_Updated3</TextReadingName>
<TextReadingId>0</TextReadingId>
</Step>
<Step>
<StepId>11D70A50-08AC-4767-A0D3-87717384FF45</StepId>
<Rank>2</Rank>
<TextReadingName>reading1</TextReadingName>
</Step>
</Steplist>');
-- DDL and sample data population, end
SELECT c.value('(StepId/text())[1]', 'uniqueidentifier') as StepId
FROM tblStepList
CROSS APPLY Data.nodes('/Steplist/Step[not(TextReadingId)]') AS t(c); --where not exists('/Steplist/Step/TextReadingId')
输出
+--------------------------------------+
| StepId |
+--------------------------------------+
| E36A3450-1C8F-44DA-B4D0-58E5BFE2A987 |
| 11D70A50-08AC-4767-A0D3-87717384FF45 |
+--------------------------------------+
【讨论】:
这就像魔术一样! @HelenAraya,请正确标记您未来的问题。这样我就不会错过你的问题。我已经更新了这个问题的标签。 ? @Yitzhak Khabinsky 检查我的 neq 问题 ***.com/q/69854955/3038042以上是关于XQuery sql 仅在存在时选择节点的主要内容,如果未能解决你的问题,请参考以下文章