这与XML Query and Retrieving Node Value 有关。
我现在可以查看XML值,并且需要将更新限制为仅这些列。我的结果继续插入NULL值。 XML中有数据。我在前面的问题中只提供了一个小集合,但是有50个节点。
我的示例如下
`Update @temptable_all
SET Headline = F.X.value('(.[@id="Headline"]/value/text())[1]','nvarchar(512)')
SET Credits = F.X.value('(.[@id="Credit"]/value/text())[1]','nvarchar(8)')
FROM @temptable_all ta
CROSS APPLY
cross apply T.Attributes.nodes('/cpCollection/group') as I(attributes)
cross apply I.attributes.nodes('property') as F(X)
WHERE ta.ArticleId = T.DataId`
更详细的XML示例:
`<cpCollection moduleId="cc5005f4-f1ea-433e-b187-8b769170eae4" dataId="0a0e2ddf-2a38-4739-9a52-000f9698978f">
<group id="Serialize" name="Serialize">
<property id="Title">
<value>One</value>
</property>
<property id="StartDate">
<value>1/1/2019</value>
</property>
<property id="EndDate">
<value>12/31/2019</value>
</property>
<property id="Headline">
<value>One, Two, Three</value>
</property>
<property id="Credit">
<value>0.25</value>
</property>
</group>
</cpCollection>`
答案 0 :(得分:0)
DECLARE @cpsys_DataCurrent TABLE ( xmltext XML);
INSERT INTO @cpsys_DataCurrent (xmltext)
VALUES
( N'<cpCollection moduleId="cc5005f4-f1ea-433e-b187-8b769170eae4" dataId="0a0e2ddf-2a38-4739-9a52-000f9698978f">
<group id="Serialize" name="Serialize">
<property id="Title">
<value>One</value>
</property>
<property id="StartDate">
<value>1/1/2019</value>
</property>
<property id="EndDate">
<value>12/31/2019</value>
</property>
<property id="Headline">
<value>One, Two, Three</value>
</property>
<property id="Credit">
<value>0.25</value>
</property>
</group>
</cpCollection>');
DECLARE @temptable_all TABLE (ArticleId nvarchar(36), HeadLine nvarchar(512), Credits nvarchar(8));
INSERT INTO @temptable_all (ArticleId,HeadLine,Credits)
VALUES
('0a0e2ddf-2a38-4739-9a52-000f9698978f',null,null)
,('0000000-000000',N'SomeThing',N'SomeMore');
--Test selected records
SELECT ta.ArticleId,
T.xmltext.value('(cpCollection/@dataId)[1]','nvarchar(100)') as DataId,
T.xmltext.value('(/cpCollection/group/property[@id="Headline"]/value/text())[1]','nvarchar(512)') AS HeadLine,
T.xmltext.value('(/cpCollection/group/property[@id="Credit"]/value/text())[1]','nvarchar(8)') AS Credits
FROM @temptable_all ta
INNER JOIN @cpsys_DataCurrent AS T
ON ta.ArticleId = T.xmltext.value('(cpCollection/@dataId)[1]','nvarchar(100)');
--Actual update query
UPDATE @temptable_all
SET Headline = T.xmltext.value('(/cpCollection/group/property[@id="Headline"]/value/text())[1]','nvarchar(512)') ,
Credits = T.xmltext.value('(/cpCollection/group/property[@id="Credit"]/value/text())[1]','nvarchar(8)')
FROM @temptable_all ta
INNER JOIN @cpsys_DataCurrent AS T
ON ta.ArticleId = T.xmltext.value('(cpCollection/@dataId)[1]','nvarchar(100)');
--Select after updating
SELECT ArticleId,HeadLine,Credits FROM @temptable_all;
“测试所选记录”的结果:
ArticleId DataId HeadLine Credits
0a0e2ddf-2a38-4739-9a52-000f9698978f 0a0e2ddf-2a38-4739-9a52-000f9698978f One, Two, Three 0.25
“更新后选择”的结果:
ArticleId HeadLine Credits
0a0e2ddf-2a38-4739-9a52-000f9698978f One, Two, Three 0.25
0000000-000000 SomeThing SomeMore