Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Getting node values from an XML column


Getting node values from an XML column

Author
Message
Chris Quinn-821458
Chris Quinn-821458
SSC-Enthusiastic
SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)

Group: General Forum Members
Points: 194 Visits: 838
I have a table with a uniqueID column (context_id) and a column containing XML data in the format shown below:


<?xml version="1.0" encoding="utf-8" ?>
<KeyValues>
   <KeyValue>
      <Key>
         <ExternalKeyName>Deal code</ExternalKeyName>
<InternalKeyName>Deal code</InternalKeyName>
<KeyId>DEALCODE</KeyId>
</Key>
      <KeyValueId>8e7230aa-ae15-4131-b4f4-c26b3da37979</KeyValueId>
      <Value>1160224</Value>
</KeyValue>
<KeyValue>
      <Key>
         <ExternalKeyName>Credit Agreement account number</ExternalKeyName>
<InternalKeyName>Credit Agreement account number</InternalKeyName>
<KeyId>CDTAGRNO</KeyId>
</Key>
<KeyValueId>db58108f-2d94-4e74-a891-923a58a4882d</KeyValueId>
      <Value>174552</Value>
</KeyValue>
   <KeyValue>
      <Key>
         <ExternalKeyName>Ticketing Deadline </ExternalKeyName>
<InternalKeyName>Ticketing Deadline Date</InternalKeyName>
         <KeyId>TICKTDLD</KeyId>
</Key>
<KeyValueId>03ee2376-8493-410a-b822-3dd94aa8a1b2</KeyValueId>
      <Value>2013-09-21 23:59:00Z</Value>
   </KeyValue>
</KeyValues>



I wish to query the KeyID and Value data into seperate rows, but am having probklems with the syntax

I have the following query :


SELECT
context_id
,a.alias.value('@KeyId','Varchar(20)') AS 'KeyID'
   ,a.alias.value('@Value','Varchar(20)') AS 'Value'
   ,a.alias.query('.') AS 'Node'
FROM (
   SELECT context_id
      ,cast(response_xml AS XML) as response_xml
   FROM dbo.tmpCMQ
   Wink r
CROSS APPLY r.response_xml.nodes('KeyValues/KeyValue') a(alias)


Results:
AE20D803-6367-4732-B625-650DAEABB9C3   NULL   NULL   <KeyValue><Key><ExternalKeyName>Deal code</ExternalKeyName><InternalKeyName>Deal code</InternalKeyName><KeyId>DEALCODE</KeyId></Key><KeyValueId>8e7230aa-ae15-4131-b4f4-c26b3da37979</KeyValueId><Value>1160224</Value></KeyValue>
AE20D803-6367-4732-B625-650DAEABB9C3   NULL   NULL   <KeyValue><Key><ExternalKeyName>Credit Agreement account number</ExternalKeyName><InternalKeyName>Credit Agreement account number</InternalKeyName><KeyId>CDTAGRNO</KeyId></Key><KeyValueId>db58108f-2d94-4e74-a891-923a58a4882d</KeyValueId><Value>174552</Value></KeyValue>
AE20D803-6367-4732-B625-650DAEABB9C3   NULL   NULL   <KeyValue><Key><ExternalKeyName>Ticketing Deadline </ExternalKeyName><InternalKeyName>Ticketing Deadline Date</InternalKeyName><KeyId>TICKTDLD</KeyId></Key><KeyValueId>03ee2376-8493-410a-b822-3dd94aa8a1b2</KeyValueId><Value>2013-09-21 23:59:00Z</Value></KeyValue>




Whatever I try, I can't get the values I want into KeyID and Value - I know it's probably a silly mistake in my syntax - can anyone help?
Mark Cowne
Mark Cowne
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2084 Visits: 22769

SELECT
context_id
,a.alias.value('(KeyValueId/text())[1]','Varchar(20)') AS 'KeyID'
   ,a.alias.value('(Value/text())[1]','Varchar(20)') AS 'Value'
   ,a.alias.query('.') AS 'Node'
FROM (
.
.
.



____________________________________________________

Deja View - The strange feeling that somewhere, sometime you've optimised this query before

How to get the best help on a forum

http://www.sqlservercentral.com/articles/Best+Practices/61537




Chris Quinn-821458
Chris Quinn-821458
SSC-Enthusiastic
SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)

Group: General Forum Members
Points: 194 Visits: 838
Many thanks! Works perfectly
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search