gpt4 book ai didi

sql - 在SQL Server中将XML元素选择到表行中

转载 作者:行者123 更新时间:2023-12-03 16:59:36 25 4
gpt4 key购买 nike

我在表中的一行中有如下所示的XML(表中有很多行):

<?xml version="1.0" encoding="UTF-8"?>
<AuditTrail>
<Action />
<ActionDetail />
<ChangesXML>
<Details>
<Object ObjectType="Data.Review_Extension" AuditType="Modified" FriendlyName="Review">
<ObjectKeys>
<ReviewExtID>21482283</ReviewExtID>
</ObjectKeys>
<Properties>
<Property name="Document Type 01" FieldName="Document_Type_01" TemplateFieldID="644140" ReviewExtensionID="214822182" PropertyType="System.String">
<OldValue />
<NewValue><![CDATA[1145]]></NewValue>
</Property>
<Property name="Document Type 02" FieldName="Document_Type_02" TemplateFieldID="644141" ReviewExtensionID="21482283" PropertyType="System.String">
<OldValue />
<NewValue><![CDATA[123]]></NewValue>
</Property>
</Properties>
</Object>
</Details>
</ChangesXML>
</AuditTrail>


我需要编写一个查询(在SQL Server 2008中),对于源表中的每一行,该查询将为XML中的EACH Property元素输出一行。因此,如果我查询上面的记录,则会得到以下结果集:

UserId    Timestamp               PropertyName
-------------------------------------------------
1 1-1-2011 00:11:22:11 Document_Type_01
2 1-1-2011 00:11:22:11 Document_Type_02


我的源表如下所示:

UserId    Timestamp               XML
--------------------------------------
1 1-1-2011 00:11:22:11 <XML>
2 4-1-2011 00:22:33:22 <XML>
3 4-2-2011 00:14:33:22 <XML>


我的第一次尝试如下所示:

SELECT  UserId, Timestamp,
CAST(AuditXml AS XML).value('(/AuditTrail/ChangesXML/Details/Object/Properties/Property/@FieldName)[1]', 'varchar(50)') AS PropertyName
FROM History order by timestamp desc


显然,这仅在只有一个property元素的情况下有效,并且在源表中每条记录仅返回一行。如何编写此查询,使其返回我要查找的结果集?

最佳答案

看一下OUTER APPLY运算符并考虑以下示例:

DECLARE @x XML = '<?xml version="1.0" encoding="UTF-8"?>
<AuditTrail>
<Action />
<ActionDetail />
<ChangesXML>
<Details>
<Object ObjectType="Data.Review_Extension" AuditType="Modified" FriendlyName="Review">
<ObjectKeys>
<ReviewExtID>21482283</ReviewExtID>
</ObjectKeys>
<Properties>
<Property name="Document Type 01" FieldName="Document_Type_01" TemplateFieldID="644140" ReviewExtensionID="214822182" PropertyType="System.String">
<OldValue />
<NewValue><![CDATA[1145]]></NewValue>
</Property>
<Property name="Document Type 02" FieldName="Document_Type_02" TemplateFieldID="644141" ReviewExtensionID="21482283" PropertyType="System.String">
<OldValue />
<NewValue><![CDATA[123]]></NewValue>
</Property>
</Properties>
</Object>
</Details>
</ChangesXML>
</AuditTrail>'

DECLARE @t TABLE (userid INT, [xml] XML)
INSERT @t VALUES(1, @x)

SELECT t.userid, r.z.value('@FieldName', 'nvarchar(MAX)')
FROM @t t
OUTER APPLY t.xml.nodes('//Property') as r(z)


希望能帮助到你

关于sql - 在SQL Server中将XML元素选择到表行中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30471049/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com