[英]SQL Query to parse XML data to multiple columns
I am new to working with XML. 我刚接触XML。 I have reviewed several posts and am having a hard time wrapping my head around this task.
我已经审查了几篇文章,并且很难完成这项任务。
I have a SQL Server 2008R2 table named tbl.AccountLogs with columns - Source, Date, ID and Details (xml data) 我有一个名为tbl.AccountLogs的SQL Server 2008R2表,该表具有以下列-源,日期,ID和详细信息(XML数据)
I am trying to parse through XML field "Details" into several columns. 我试图通过XML字段“ Details”解析为几列。 The field can have numerous tags and nested tags.
该字段可以具有许多标签和嵌套标签。
Here is an example: 这是一个例子:
<errors>
<error date = '1394746874'>
<detail id = '3342867234'>
<arg type = '0'><![CDATA[512,107,741,184]]></arg>
<arg type = '0'><![CDATA[274,877,906,944]]></arg>
<arg type = '0'><![CDATA[J:\]]></arg>
<arg type = '0'><![CDATA[TestLine.img]]></arg>
</detail>
</error>
<error date = '1394747022'>
<detail id = '3342867234'>
<arg type = '0'><![CDATA[28,594,585,600]]></arg>
<arg type = '0'><![CDATA[21,474,836,480]]></arg>
<arg type = '0'><![CDATA[C:\Safe\]]></arg>
<arg type = '0'><![CDATA[{BigMonster}]]></arg>
</detail>
</error>
</errors>
As mentioned before, the number of "errors" can change per record, but the "arg type" and "detail id" are consistent. 如前所述,每个记录的“错误”数可以更改,但是“ arg类型”和“详细信息ID”是一致的。
Example output would be:
输出示例如下:
Any assistance would be greatly appreciated! 任何帮助将不胜感激!
Try this, hope it helps you move in the right direction: 试试这个,希望它可以帮助您朝正确的方向前进:
--if temp table exists drop it
IF OBJECT_ID('tempdb..#tmp') IS NOT NULL DROP TABLE #tmp
go
CREATE TABLE #tmp ( your_xml NVARCHAR(MAX) )
GO
--insert data
INSERT INTO #tmp SELECT '<errors>
<error date = ''1394746874''>
<detail id = ''3342867234''>
<arg type = ''0''><![CDATA[512,107,741,184]]></arg>
<arg type = ''0''><![CDATA[274,877,906,944]]></arg>
<arg type = ''0''><![CDATA[J:\]]></arg>
<arg type = ''0''><![CDATA[TestLine.img]]></arg>
</detail>
</error>
<error date = ''1394747022''>
<detail id = ''3342867234''>
<arg type = ''0''><![CDATA[28,594,585,600]]></arg>
<arg type = ''0''><![CDATA[21,474,836,480]]></arg>
<arg type = ''0''><![CDATA[C:\Safe\]]></arg>
<arg type = ''0''><![CDATA[{BigMonster}]]></arg>
</detail>
</error>
</errors>'--END of xml
SELECT
'Record'+Convert(nvarchar(50), (ROW_NUMBER() OVER (ORDER BY x.y.value('(./detail[1]/arg)[1]', 'nvarchar(100)')))) AS [Source],
x.y.value('@date', 'nvarchar(100)') AS [Date],
x.y.value('./detail[1]/@id', 'nvarchar(100)') AS ID,
x.y.value('(./detail[1]/arg)[1]', 'nvarchar(100)') AS Detail1,
x.y.value('(./detail[1]/arg)[2]', 'nvarchar(100)') AS Detail2,
x.y.value('(./detail[1]/arg)[3]', 'nvarchar(100)') AS Detail3,
x.y.value('(./detail[1]/arg)[4]', 'nvarchar(100)') AS Detail4
FROM
(
SELECT CAST( your_xml AS XML ) AS your_xml
FROM #tmp
) t
CROSS APPLY t.your_xml.nodes('//errors/error') x(y)
Basically you use CROSS APPLY and using the Properties.value to retrieve the values of the XML. 基本上,您使用CROSS APPLY并使用Properties.value来检索XML的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.