I have TableC and TableA. I want all the records from TableC whereas only matching records from TableA so I'm using 'left join'. The problem is that TableA has an XML column. The XML in that column has following structure
<x:main xmlns:x="x-elements">
<x:rules>
<x:obj>
<ruleName>name1</ruleName>
<createdBy>userA</createdBy>
<type>bbb</type>
</x:obj>
<x:obj>
<ruleName>name2</ruleName>
<createdBy>userA</createdBy>
<type>ccc</type>
</x:obj>
</x:rules>
<x:info>
<x:obj>
<target>ftp:1</target>
<user>userB</user>
</x:obj>
<x:obj>
<target>ftp:3</target>
<user>userA</user>
</x:obj>
</x:info>
</x:main>
I want to get createdBy
from XML column for each row where equivalent type
is 'ccc'.
Below is my effort
with xmlnamespaces ('x-elements' as x),
res1 as (select x.xmlCol.value('(createdBy)[1]', 'varchar(500)') prop1
from TableC c
left join TableA a
cross apply a.xCol.nodes('x:main/x:rules/x:obj') x(xmlCol)
on c.Id = a.Id
where x.xmlCol.value('(type)[1]', 'varchar(500)') = 'ccc')
select
c.Name,
(select prop1 from res1) prop1
from TableC c
left join TableA a
on c.Id = a.Id
However, I'm getting an error stating
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Can anyone please guide on how to achieve what I'm trying to do here?
P.S Later I would also like to get 'target' from XML column for each row where equivalent user
is 'userA'.