Within a PL/SQL procedure, wrap a query or refcursor in HTML table

后端 未结 2 792
甜味超标
甜味超标 2021-01-05 22:20

It seems really easy if you are using SQL*Plus to use

SQL> set markup html on;

and get some lovely results to the SQL*Plus window. we

2条回答
  •  萌比男神i
    2021-01-05 23:02

    From posting over On the DBA site I got close to the answer that I was looking for. Tom Kyte has a great blog post with a function that does exactly what I was hoping for. In short here is what I implemented:

    I created a function that took a sys_refcursor as a variable:

    CREATE OR REPLACE FUNCTION fncRefCursor2HTML(rf SYS_REFCURSOR)  RETURN CLOB
    IS
        lRetVal      CLOB;
        lHTMLOutput  XMLType; 
        lXSL         CLOB;
        lXMLData     XMLType;
    
        lContext     DBMS_XMLGEN.CTXHANDLE;
    BEGIN
        -- get a handle on the ref cursor --
        lContext := DBMS_XMLGEN.NEWCONTEXT(rf);
        -- setNullHandling to 1 (or 2) to allow null columns to be displayed --
        DBMS_XMLGEN.setNullHandling(lContext,1);
        -- create XML from ref cursor --
        lXMLData := DBMS_XMLGEN.GETXMLTYPE(lContext,DBMS_XMLGEN.NONE);
    
        -- this is a generic XSL for Oracle's default XML row and rowset tags --
        -- " " is a non-breaking space --
        lXSL := lXSL || q'[]';
        lXSL := lXSL || q'[]';
        lXSL := lXSL || q'[ ]';
        lXSL := lXSL || q'[ ]';
        lXSL := lXSL || q'[ ]';
        lXSL := lXSL || q'[  ]';
        lXSL := lXSL || q'[   ]';
        lXSL := lXSL || q'[     ]';
        lXSL := lXSL || q'[      ]';
        lXSL := lXSL || q'[       ]';
        lXSL := lXSL || q'[      ]';
        lXSL := lXSL || q'[     ]';
        lXSL := lXSL || q'[     ]';
        lXSL := lXSL || q'[      ]';    
        lXSL := lXSL || q'[       ]';
        lXSL := lXSL || q'[        ]';
        lXSL := lXSL || q'[       ]';
        lXSL := lXSL || q'[      ]';
        lXSL := lXSL || q'[     ]';
        lXSL := lXSL || q'[   
    ]'; lXSL := lXSL || q'[ ]'; lXSL := lXSL || q'[ ]'; lXSL := lXSL || q'[
    ]'; lXSL := lXSL || q'[
    ]'; -- XSL transformation to convert XML to HTML -- lHTMLOutput := lXMLData.transform(XMLType(lXSL)); -- convert XMLType to Clob -- lRetVal := lHTMLOutput.getClobVal(); RETURN lRetVal; END;

    Then to test it in a Test window in PL/SQL Developer

    declare 
      l_cursor sys_refcursor;
    begin
      open l_cursor for select * from employees;
      :x:= fncRefCursor2HTML(l_cursor);
      close l_cursor;  
    end;
    

    This is something that I have been hoping to find for a long time. THANKS Tom Kyte!

提交回复
热议问题