gpt4 book ai didi

xml - SQL Server 查询返回 Xml 和 Html

转载 作者:行者123 更新时间:2023-12-04 05:53:25 24 4
gpt4 key购买 nike

我们正在将应用程序从 oracle 迁移到 SQL Server。

在 Oracle 中,用于返回嵌入了一些 HTML 标记的 XML 的过程。

来源如下。

    SELECT XMLAGG (
XMLFOREST (
XMLELEMENT ("a", XMLATTRIBUTES ('#' AS "href"), MODULENAME) "h3",
XMLELEMENT (
"ul",
XMLAGG (
XMLELEMENT (
"li",
XMLELEMENT (
"a",
XMLATTRIBUTES (
'#' AS "href",
'pageclick('''
|| SCREENPATH
|| ''','|| SCREENID||')' AS "onclick"),
SCREENNAME))
ORDER BY SORDER ASC)) "div")
ORDER BY MORDER ASC).getclobval ()

FROM (SELECT B.SUBMODULEID MODULEID,
C.PAGEID SCREENID,
C.PAGENAME SCREENNAME,
C.PAGECODE,
B.SUBMODULEID,
B.SUBMODULENAME MODULENAME,
C.PAGEURLL1 SCREENPATH,
C.ORDERNO SORDER,
B.ORDERNO MORDER
FROM SETP.SM_PAGES C,
SETP.SM_MODULES A,
SETP.SM_SUB_MODULES B,
SETP.SM_USRPRIVHDR D,
SETP.SM_USRPRIVDTL E
WHERE D.USRPRIVID = E.USRPRIVID
AND C.PAGEID = E.PAGEID
AND B.MODULEID = A.MODULEID
AND C.SUBMODULEID = B.SUBMODULEID
AND D.USRID = 2) page
GROUP BY MODULENAME, MORDER;

这是输出
<h3>
<a href="#">Masters</a>
</h3>
<div>
<ul>

<li>
<a href="#" onclick="pageclick(&apos;WmsSetup.aspx/BaggingConfig&apos;,1177)">Bagging Configuration</a>
</li>
<li>
<a href="#" onclick="pageclick(&apos;WMS.aspx/Items&apos;,1171)">Item Master</a>
</li>

</ul>
</div>

在不使用 XSLT 的情况下,我一直在努力在 SQL Server 中实现相同的目标。
有任何想法吗 ?

感谢和问候

最佳答案

这看起来应该适合你。

;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>

关于xml - SQL Server 查询返回 Xml 和 Html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9803887/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com