Вы можете использовать union
для объединения разных узлов. Это довольно сложно:
select p.nodeid as 'node/@id'
, (
select [@name], [@value]
from (
select 'UID' as '@name'
, cast(uid as varchar(10)) as '@value'
, nodeid
from @profiles
union all
select 'Name' as '@name'
, name as '@value'
, nodeid
from @profiles
) sub
where sub.nodeid = p.nodeid
for xml path('att'), type
) as node
, null as 'edge/@source'
, null as 'edge/@target'
from @profiles p
union all
select null as 'node/@node'
, null as node
, g.uid1 as 'edge/@source'
, g.uid2 as 'edge/@target'
from @graph g
for xml path(''), root('graph'), type
Данные испытаний:
declare @graph table (uid1 int, uid2 int)
declare @profiles table (nodeid int, uid int, name varchar(25))
insert into @graph values (12, 23), (12,32), (41,51), (32,41)
insert into @profiles values (1,12,'Robs'), (2,23,'Jones'), (3,32,'Lim'),
(4,41,'Teo'), (5,51,'Zacks')