ali soltani ali soltani - 3 months ago 18
SQL Question

Get XML child node value with dynamic parent in SQL Server

I have a

Process
table in SQL Server like this:

enter image description here

workflowXML
column has values like this:

<bpmn:definitions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL"
xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI"
xmlns:dc="http://www.omg.org/spec/DD/20100524/DC"
xmlns:di="http://www.omg.org/spec/DD/20100524/DI"
id="Definitions_1" targetNamespace="http://bpmn.io/schema/bpmn">
<bpmn:process id="Process_1" isExecutable="false">
<bpmn:startEvent id="StartEvent_1rin5au">
<bpmn:outgoing>SequenceFlow_1q36672</bpmn:outgoing>
</bpmn:startEvent>
<bpmn:userTask id="UserTask_038517r" name="addRequest">
<bpmn:incoming>SequenceFlow_1q36672</bpmn:incoming>
<bpmn:outgoing>SequenceFlow_0dpq0oe</bpmn:outgoing>
</bpmn:userTask>
<bpmn:serviceTask id="ServiceTask_17e1u09" name="checkRequest">
<bpmn:incoming>SequenceFlow_1q36672</bpmn:incoming>
<bpmn:outgoing>SequenceFlow_1233bp0</bpmn:outgoing>
</bpmn:serviceTask>
</bpmn:process>
</bpmn:definitions>


I need a SQL query to get
id
node that
incoming
value be equal with
outgoing
value in
bpmn:startEvent
node.

I test this subquery:

;WITH XMLNAMESPACES('http://www.omg.org/spec/BPMN/20100524/MODEL' AS bpmn)
select
p.workflowXML.value('(bpmn:definitions/bpmn:process/bpmn:userTask/bpmn:incoming)[1]', 'nvarchar(max)')
from
Process as p


But this code only returns result for
bpmn:userTask
. I used other subquery:

;WITH XMLNAMESPACES('http://www.omg.org/spec/BPMN/20100524/MODEL' AS bpmn)
select
p.workflowXML.value('(bpmn:definitions/bpmn:process/../bpmn:incoming)[1]', 'nvarchar(max)')
from
Process as p


But it returns
null
.

My query is like this:

declare @SequenceFlow nvarchar(max)
;WITH XMLNAMESPACES('http://www.omg.org/spec/BPMN/20100524/MODEL' AS bpmn)
select @SequenceFlow =
(
SELECT p.workflowXML.value('(/bpmn:definitions/bpmn:process/bpmn:startEvent/bpmn:outgoing)[1]','nvarchar(max)')
from Process as p
where ID = 1
)

;WITH XMLNAMESPACES('http://www.omg.org/spec/BPMN/20100524/MODEL' AS bpmn)
select p.workflowXML.value('(bpmn:definitions/bpmn:process/../bpmn:incoming)[1]','nvarchar(max)')
from Process as p
where p.workflowXML.value('(bpmn:definitions/bpmn:process/../bpmn:incoming)[1]','nvarchar(max)') = @SequenceFlow


It would be very helpful if someone could explain solution for this problem.

Thanks.

Answer

You can use * to reference element of arbitrary name :

;WITH XMLNAMESPACES('http://www.omg.org/spec/BPMN/20100524/MODEL' AS bpmn) 
select t.c.value('@id','nvarchar(max)')
from Process as p
    cross apply p.DiagramXML.nodes('bpmn:definitions/bpmn:process/*') AS t(c)
where t.c.value('bpmn:incoming[1]', 'nvarchar(max)') = @SequenceFlow

Notice that cross apply is used to shred XML data at child of bpmn:process element level. This allows the query to return multiple rows from single XML column data.

Comments