XQuery sql Join在join不匹配时插入空节点
Posted
技术标签:
【中文标题】XQuery sql Join在join不匹配时插入空节点【英文标题】:XQuery sql Join inserts empty node when join does not match 【发布时间】:2021-12-19 14:37:58 【问题描述】:我正在使用 SQL Server 2012,并且正在寻找一种通过从另一个表中查找值来更新我的 xml 列的方法。
我有以下架构:
USE tempdb;
GO
DROP TABLE IF EXISTS [dbo].[tblstepid];
CREATE TABLE [dbo].[tblstepid](
[stepid] [uniqueidentifier] NOT NULL,
[name] nvarchar(32) NOT NULL
);
INSERT INTO dbo.tblstepid ([stepid], name ) VALUES ('E36A3450-1C8F-44DA-B4D0-58E5BFE2A987','step1')
INSERT INTO dbo.tblstepid ([stepid], name ) VALUES ('11D70A50-08AC-4767-A0D3-87717384FF45','step2')
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>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>
<TextReadingName>reading1</TextReadingName>
</Step>
</Steplist>')
现在我想更新另一个表中具有匹配步骤 id 的所有步骤,如下所示:
UPDATE sl
SET Data = (
SELECT v.Step.query('
<Step>./*,
<stepname>sql:column("sr.name")</stepname>
</Step>
')
FROM sl.Data.nodes('/Steplist/Step') v(Step)
left JOIN tblStepID sr ON sr.StepId = v.Step.value('(StepId/text())[1]','uniqueidentifier')
FOR XML PATH(''), ROOT('Steplist'), TYPE
)
FROM tblStepList sl;
我的结果 xml 输出有一个我不想要的空节点。 我怎样才能编写我的联接不为我添加空节点,如果没有匹配项也不设置数据,这意味着我的表 xml 中的第二条记录不应该被触及?
我要解决的问题是,我正在尝试通过从另一个表连接来更新具有数百万条记录的表的 xml,并且如果连接不匹配,我不希望更新语句接触数据。
<Steplist>
<Step>
<StepId>e36a3450-1c8f-44da-b4d0-58e5bfe2a987</StepId>
<Rank>1</Rank>
<IsComplete>false</IsComplete>
<TextReadingName>bug-8588_Updated3</TextReadingName>
<stepname>step1</stepname>
</Step>
<Step>
<StepId>4078c1b1-71ea-4578-ba61-d2f6a5126ba1</StepId>
<Rank>2</Rank>
<TextReadingName>reading1</TextReadingName>
<TextReadingId>12</TextReadingId>
<stepname />
</Step>
</Steplist>
我的预期输出如下:
<Steplist>
<Step>
<StepId>e36a3450-1c8f-44da-b4d0-58e5bfe2a987</StepId>
<Rank>1</Rank>
<IsComplete>false</IsComplete>
<TextReadingName>bug-8588_Updated3</TextReadingName>
<stepname>step1</stepname>
</Step>
<Step>
<StepId>4078c1b1-71ea-4578-ba61-d2f6a5126ba1</StepId>
<Rank>2</Rank>
<TextReadingName>reading1</TextReadingName>
<TextReadingId>12</TextReadingId>
</Step>
</Steplist>
【问题讨论】:
请使用 DDL 和 tblStepID 表的样本数据填充更新您的问题。您总是需要提供一个最小的可重现示例 @YitzhakKhabinsky 我刚刚更新了它。 【参考方案1】:请尝试以下解决方案。
我添加了几个 XQuery if/else
语句以防止出现空的 <stepname />
标记。
SQL
USE tempdb;
GO
-- DDL and sample data population, start
DROP TABLE IF EXISTS [dbo].[tblStepID];
DROP TABLE IF EXISTS [dbo].[tblStepList];
CREATE TABLE dbo.tblstepid(
stepid uniqueidentifier NOT NULL,
[name] nvarchar(32) NOT NULL
);
INSERT INTO dbo.tblStepID (StepId, [Name]) VALUES
('e36a3450-1c8f-44da-b4d0-58e5bfe2a987', 'step1'),
('4078c1b1-71ea-4578-ba61-d2f6a5126ba1', 'step2');
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>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>
<TextReadingName>reading1</TextReadingName>
</Step>
</Steplist>');
-- DDL and sample data population, end
UPDATE sl
SET Data = (
SELECT v.Step.query('
<Step>./*,
if (not(./stepname)) then
if (not(empty(sql:column("sr.name")))) then <stepname>sql:column("sr.name")</stepname>
else ()
else ()
</Step>
')
FROM sl.Data.nodes('/Steplist/Step') AS v(Step)
LEFT JOIN tblStepID sr ON sr.StepId = v.Step.value('(StepId/text())[1]','uniqueidentifier')
FOR XML PATH(''), ROOT('Steplist'), TYPE
)
FROM tblStepList AS sl;
-- test
SELECT * FROM dbo.tblStepList;
【讨论】:
这不会解决我的问题,因为如果连接的字段不匹配,这仍然会重置数据而没有任何变化。我希望不为没有任何指令匹配的第 2 行调用该集合? 这种情况可以尝试将LEFT JOIN tblStepID sr
改为INNER JOIN
这会将空值分配给我的 xml。
我没有其他想法了。最初提出的解决方案应该可以工作。
您需要LEFT JOIN
,因为您需要替换整个 XML。我建议将CROSS APPLY
与WHERE
过滤器一起使用,因此只有在实际需要进行任何更改时才更新。也可能更快地切换条件的顺序,并将它们合并到一个 if(... and ...)
以上是关于XQuery sql Join在join不匹配时插入空节点的主要内容,如果未能解决你的问题,请参考以下文章
SQL SERVER LEFT JOIN, INNER JOIN, RIGHT JOIN
SQL的JOIN语法解析(inner join, left join, right join, full outer join的区别)