declare @ParentID int
set @ParentID = 1
select a.name,
bc.descb,
bc.descc
from TableA as a
cross join (select b.descb,
c.descc
from (select *,
row_number() over(order by b.bkey) as rn
from TableB as b
where b.parentid = @parentid) as b
full outer join
(select *,
row_number() over(order by c.ckey) as rn
from TableC as c
where c.parentid = @parentid) as c
on b.rn = c.rn) as bc
where a.parentid = @parentid
Попробуйте здесь: http://data.stackexchange.com/stackoverflow/qt/112538/
Редактировать: Версия, использующая ExternalKey для запроса нескольких идентификаторов ParentID
Рекомендуемые индексы:
create index IX_B_ParentID on TableB(ParentID) include (DescB)
create index IX_C_ParentID on TableC(ParentID) include (DescC)
Я бы создал табличную переменную, которая содержит ParentID, соответствующий ExternalKey, а затем использовал бы ее вместо TableA в запросе.
declare @ExternalKey int = 1
declare @T table(ParentID int primary key, Name varchar(20))
insert into @T (ParentID, Name)
select ParentID, NAme
from TableA
where ExternalKey = @ExternalKey
select a.name,
bc.descb,
bc.descc
from @T as a
inner join (select b.descb,
c.descc,
coalesce(b.ParentID, c.ParentID) as ParentID
from (select b.ParentID,
b.DescB,
row_number() over(partition by b.ParentID order by b.bkey) as rn
from TableB as b
where b.parentid in (select ParentID from @T)) as b
full outer join
(select c.ParentID,
c.DescC,
row_number() over(partition by c.ParentID order by c.ckey) as rn
from TableC as c
where c.parentid in (select ParentID from @T)) as c
on b.rn = c.rn and
b.ParentID = c.ParentID) as bc
on a.ParentID = bc.ParentID