2012-03-21 86 views
2

我們正在將一個應用程序從oracle遷移到SQL Server。返回Xml和Html的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> 

我一直在分裂我的頭髮,實現SQL Server中的相同,而無需使用XSLT。 任何想法?

感謝&問候

+0

的SQL Server版本? – JeffO 2012-03-21 12:00:48

+0

SQL Server 2005 Prof/Ent。 – Deb 2012-03-21 12:19:26

+0

現在你有15個聲望點 - 所以你可以** upvote ** Mikael的優秀答案! :-) – 2012-03-21 12:40:45

回答

3

這看起來像它應該爲你工作。

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

簡直無語!萬分感謝。 – Deb 2012-03-21 12:26:51

+0

不客氣。 – 2012-03-21 12:30:37

+0

我完全不知道這是如何工作的......但我能夠修改它以適合我自己的查詢。非常感謝你。 – WuHoUnited 2014-02-18 19:47:42