简体   繁体   中英

Shredding XML in SQL Server from NTEXT

Hopefully this is an easy one for experts :)

I have a column containing XML (at the bottom of this post) - the column is of ntext datatype. Rather than displaying the entire XML string in a column, I want to shred it into multiple columns. The database is SQL Server 2008 R2. Column datatype cannot be changed (MS application DB)

I've tried multiple variations of xpath queries but I keep getting null values. Hopefully it's just the way I'm referencing it which is incorrect. Any help would be greatly appreciated as I can't get my head around XML it seems (not through lack of trying!):

XML (example from one record in the result - I've split it up for ease of reading):

<?xml version="1.0" encoding="utf-16"?>  
<q1:ErrorInfo ErrorCode="1073744938" DetailedCode="0" DetailedSource="0" ExceptionDetails="" xmlns:q1="http://schemas.microsoft.com/2003/dls/GenericAgentStatus.xsd">    
<q1:Parameter Name="datasourceid" Value="48c3db91-4ba9-46a5-820b-a2ab2c0733aa" />    
<q1:Parameter Name="doesalertneedstroubleshootuiformoredetails" Value="False" />    
<q1:Parameter Name="failurecount" Value="1" />    
<q1:Parameter Name="customparameterformom" Value="86924" />    
<q1:Parameter Name="datasourcename" Value="servername\database1" />    
<q1:Parameter Name="protectedgroup" Value="ProtectionGroup1 Servers - servername.domain" />    
<q1:Parameter Name="servername" Value="servername.domain" />  
</q1:ErrorInfo>

SQL which I'm working with (null values returned):

WITH XMLNAMESPACES('http://schemas.microsoft.com/2003/dls/GenericAgentStatus.xsd' as q1)                        
select cast(ErrorXml as XML).value('(/q1:ErrorInfo/q1:Parameter[6]/q1:value[0]/text())[1]','varchar(MAX)') as [servername]
from
target_table

At the end of all this I would like to split out servername, protected group, datasource name.

Any pointers on where I'm going wrong would be massively appreciated.

Thanks in advance

Well unfortunatley, it seems I can't mark a comment as an answer - took a look on SO forums and this is by design. Kudos to both commentors for this post.

The solution I'm using is from har07 but I did try the solution from Marcus and that worked perfectly as well.

My reworked code now looks like this ... and works perfectly:

    select cast(ls.ErrorXml as XML).value('(/q1:ErrorInfo/q1:Parameter[6]/@Value)[1]','varchar(MAX)') as [ServerName],
cast(ls.ErrorXml as XML).value('(/q1:ErrorInfo/q1:Parameter[5]/@Value)[1]','varchar(MAX)') as [ProtectedGroup],
cast(ls.ErrorXml as XML).value('(/q1:ErrorInfo/q1:Parameter[4]/@Value)[1]','varchar(MAX)') as [DataSourceName],

...

Thanks loads again - saved me so much time and sanity!

一般来说,在XPath中,您可以使用@attributeName来选择XML属性,您可以尝试以下方式:

/q1:ErrorInfo/q1:Parameter[6]/@Value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM