Reputation: 1422
I have a table with the following content
Id | Guid | XmlDefinitionId
1 | 5a0bfc84-13ec-4497-93e0-655e57d4b482 | 1
2 | e28e786b-0856-40b6-8189-0fbd68aa3e45 | 1
And in another table the following XML structure stored:
<ActionActivity DisplayName="DisplayName 1" IsSkipped="False" Id="5a0bfc84-13ec-4497-93e0-655e57d4b482">...</ActionActivity>
<p:Sequence DisplayName="Prerequisites">
<ActionActivity DisplayName="Inner DisplayName 1" IsSkipped="False" Id="e28e786b-0856-40b6-8189-0fbd68aa3e45">...</ActionActivity>
</p:Sequence>
<ActionActivity DisplayName="DisplayName 2" IsSkipped="False" Id="dcc936dd-73c9-43cc-beb4-c636647d4851">...</ActionActivity>
The table containing the XML have the following structure:
Id | XML
1 | (XML Structure defined above here)
Based on the Guid I want to show the displayname. At the moment I have the following query that returns null at the moment. Later I want for every guid from the first table show the displayname.
SELECT
Workflow
,CAST(Workflow as XML).value('data(//ActionActivity[@Id="73c9-43cc-beb4-c636647d4851"])[1]', 'nvarchar(50)') as displayname
FROM SerializedData
Anyone ideas to show the displayname with a sql query?
Upvotes: 4
Views: 1285
Reputation: 89285
Assuming that the XML stored in XML typed column, you can do this way -otherwise you'll need to CAST
the column to XML
- :
SELECT
g.guid, x.display_name
FROM GuidTable g
INNER JOIN
(
SELECT
t.id as 'xml_id'
, c.value('@Id', 'varchar(max)') as 'guid'
, c.value('@DisplayName', 'varchar(max)') as 'display_name'
FROM XmlTable t
CROSS APPLY t.xml.nodes('//ActionActivity') as aa(c)
) x on x.guid = g.guid and x.xml_id = g.xmldefinitionid
Basically, above query shred the XML at ActionActivity
node. And then joins shredded data with GuidTable
on guid
and xmldefinitionid
columns.
output :
Upvotes: 2