Похоже, это должно работать для вас.
;with C as
(
-- Your derived table goes here
SELECT B.SUBMODULEID MODULEID,
C.PAGEID SCREENID,
.
.
FROM SETP.SM_PAGES C,
.
.
)
select '#' as "h3/a/@href",
MODULENAME as "h3/a",
(
select '#' as "a/@href",
'pageclick('''+SCREENPATH+''','+SCREENID+')' as "a/@onclick",
SCREENNAME as "a"
from C as C2
where C1.MODULENAME = C2.MODULENAME and
C1.MORDER = C2.MORDER
for xml path('li'), root('ul'), type
) as "div"
from C as C1
group by MODULENAME, MORDER
for xml path('')
Я использую общее табличное выражение (CTE), потому что мне нужно повторно использовать производную таблицу для построения внутренних частей XML.
Вот рабочий пример с табличной переменной вместо вашего подзапроса.
declare @T table
(
MODULEID int,
SCREENID varchar(10),
SCREENNAME varchar(35),
PAGECODE varchar(10),
SUBMODULEID int,
MODULENAME varchar(10),
SCREENPATH varchar(35),
SORDER int,
MORDER int
)
insert into @T values
(1, '1177', 'Bagging Configuration', 'page', 3, 'Masters', 'WmsSetup.aspx/BaggingConfig', 4, 5)
insert into @T values
(1, '1171', 'Item Master', 'page', 3, 'Masters', 'WMS.aspx/Items', 4, 5)
;with C as
(
select *
from @T
)
select '#' as "h3/a/@href",
MODULENAME as "h3/a",
(
select '#' as "a/@href",
'pageclick('''+SCREENPATH+''','+SCREENID+')' as "a/@onclick",
SCREENNAME as "a"
from C as C2
where C1.MODULENAME = C2.MODULENAME and
C1.MORDER = C2.MORDER
for xml path('li'), root('ul'), type
) as "div"
from C as C1
group by MODULENAME, MORDER
for xml path('')
Результат:
<h3>
<a href="#">Masters</a>
</h3>
<div>
<ul>
<li>
<a href="#" onclick="pageclick('WmsSetup.aspx/BaggingConfig',1177)">Bagging Configuration</a>
</li>
<li>
<a href="#" onclick="pageclick('WMS.aspx/Items',1171)">Item Master</a>
</li>
</ul>
</div>