Oracle generates SQL query results as HTML web pages
- method one:
- Method 2:
- Implementation of method one:
-
- 1. Log in to the Oracle database to create a temporary table scott_emp for use in this drill.
- 2. Create the function sql_to_html_xslt(), input the SQL and title to be executed, and then return the SQL query result as a CLOB object in html format.
- 3. Log in as an administrator, create a directory html_dir and authorize it to the current user who needs to use it.
- 4. Create and save clob_to_file(), convert CLOB information into a file (HTML)
- Implementation of the second method:
method one:
1. The table data is generated as a CLOB object in html format
2. Convert CLOB information into files (HTML)
Method 2:
Execute a batch program on the command line to generate HTML files
Let's look at it in detail:
Implementation of method one:
1. Log in to the Oracle database to create a temporary table scott_emp for use in this drill.
The scott_emp table is the data table in this drill, created based on the scott.emp table.
CREATE TABLE scott_emp AS select * from scott.emp;
2. Create the function sql_to_html_xslt(), input the SQL and title to be executed, and then return the SQL query result as a CLOB object in html format.
2.1. Create function sql_to_html_xslt()
There are two ways to create a function, one is to execute the SQL script to create the function from the SQL window, and the other is to execute the script file to create the function on the command line.
The difference between the two is,In the SQL window, the escape symbol representing a space in the html in the SQL script “ ”
needs to be changed “& ”
, that is, one more “&”
symbol.
2.1.1, SQL window executes SQL script creation function sql_to_html_xslt()
You can directly open a SQL window with PL/SQL, copy the following SQL script and execute it:
create or replace function sql_to_html_xslt(p_sql in varchar2, p_title varchar2 default null)
return clob as
/*
Copyright DarkAthena([email protected])
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/* author:DarkAthena
name:query sql to a html-table (with xslt)
date:2021-10-28
EMAIL:[email protected]
example 1:
select sql_to_html_xslt(Q'{select * from job_history}') html_table
from dual;
example 2:
select sql_to_html_xslt(Q'{select * from job_history}','this is title') html_table
from dual;
*/
l_ctx dbms_xmlgen.ctxhandle;
l_num_rows pls_integer;
l_xml xmltype;
l_html xmltype;
l_returnvalue clob;
l_xml_to_html_stylesheet varchar2(4000);
l_css varchar2(4000);
begin
l_xml_to_html_stylesheet := q'^<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<table border="1">
<xsl:apply-templates select="ROWSET/ROW[1]" />
</table>
</xsl:template>
<xsl:template match="ROW">
<tr><xsl:apply-templates mode="th" /></tr>
<xsl:apply-templates select="../ROW" mode="td" />
</xsl:template>
<xsl:template match="ROW/*" mode="th">
<th><xsl:value-of select="local-name()" /></th>
</xsl:template>
<xsl:template match="ROW" mode="td">
<tr><xsl:apply-templates /></tr>
</xsl:template>
<xsl:template match="ROW/*">
<td><xsl:apply-templates /></td>
</xsl:template>
</xsl:stylesheet>^';
l_css := '<style type=''text/css''>
body {font:10pt Arial,Helvetica,sans-serif; color:black; background:White;}
p {font:10pt Arial,Helvetica,sans-serif; color:black; background:White;}
table,tr,td {font:10pt Arial,Helvetica,sans-serif; color:Black; background:#f7f7e7; padding:0px 0px 0px 0px; margin:0px 0px 0px 0px;}
th {font:bold 10pt Arial,Helvetica,sans-serif; color:#336699; background:#cccc99; padding:0px 0px 0px 0px;}
h1 {font:16pt Arial,Helvetica,Geneva,sans-serif; color:#336699; background-color:White; border-bottom:1px solid #cccc99; margin-top:0pt; margin-bottom:0pt; padding:0px 0px 0px 0px;}
h2 {font:bold 10pt Arial,Helvetica,Geneva,sans-serif; color:#336699; background-color:White; margin-top:4pt; margin-bottom:0pt;}
a {font:9pt Arial,Helvetica,sans-serif; color:#663300; background:#ffffff; margin-top:0pt; margin-bottom:0pt; vertical-align:top;}
</style>'; ---CSS ---- from SQLPLUS spool html style
l_returnvalue := '<!DOCTYPE HTML><html><head><body>' || l_css || '<h1>' ||
p_title || '</h1>';
l_ctx := dbms_xmlgen.newcontext(p_sql);
dbms_xmlgen.setnullhandling(l_ctx, dbms_xmlgen.empty_tag);
l_xml := dbms_xmlgen.getxmltype(l_ctx, dbms_xmlgen.none);
--dbms_output.put_line(l_xml.getClobVal());
l_num_rows := dbms_xmlgen.getnumrowsprocessed(l_ctx);
dbms_xmlgen.closecontext(l_ctx);
if l_num_rows > 0 then
l_html := l_xml.transform(xmltype(l_xml_to_html_stylesheet));
dbms_lob.append(l_returnvalue, l_html.getclobval());
end if;
dbms_lob.append(l_returnvalue, '</body>' || chr(10) || '</html>');
return replace(l_returnvalue,' <td/>',' <td>& </td>');
exception
when others then
raise;
end;
/
2.1.2. Execute the script file sql_to_html_xslt.fnc on the command line to create the function sql_to_html_xslt()
2.1.2.1, Download the script file from the network disk: sql_to_html_xslt.fnc
Online disk download: https://pan.baidu.com/s/1LHgwPqa01_Ig07deJP6vZA?pwd=yyds
2.1.2.2, command line execution script file creation function sql_to_html_xslt()
After downloading the file (sql_to_html_xslt.fnc),
open a command line window with PL/SQL and enter the command:
@D:\tmp\sql_to_html_xslt.fnc
(Note: The actual directory name is modified according to your own file location, and there should be no spaces in the directory where the file is located).
The execution process is the result as shown in the figure below:
2.2. Call the function sql_to_html_xslt()
2.2.1. Execute SQL query (query with title parameter)
SELECT sql_to_html_xslt(q'{select * from scott_emp}', 'sql_to_html') FROM dual;
2.2.2. Execute SQL query (query without title parameter)
SELECT sql_to_html_xslt(q'{select * from scott_emp}') FROM dual;
3. Log in as an administrator, create a directory html_dir and authorize it to the current user who needs to use it.
Log in as " sys@TZQ AS SYSDBA " to create a directory and authorize it to the current user:
CREATE OR REPLACE DIRECTORY html_dir AS 'D:\tmp\html';
GRANT READ,WRITE ON DIRECTORY html_dir TO LOG;
4. Create and save clob_to_file(), convert CLOB information into a file (HTML)
4.1. Create and save clob_to_file()
clob_to_file() has saved the code as follows:
CREATE OR REPLACE PROCEDURE clob_to_file(p_dir IN VARCHAR2,
p_file IN VARCHAR2,
p_clob IN CLOB) AS
l_output utl_file.file_type;
l_amt NUMBER DEFAULT 32000;
l_offset NUMBER DEFAULT 1;
l_length NUMBER DEFAULT nvl(dbms_lob.getlength(p_clob)
,0);
BEGIN
l_output := utl_file.fopen(p_dir
,p_file
,'w'
,32760);
WHILE (l_offset < l_length) LOOP
utl_file.put(l_output
,dbms_lob.substr(p_clob
,l_amt
,l_offset));
utl_file.fflush(l_output);
l_offset := l_offset + l_amt;
END LOOP;
utl_file.new_line(l_output);
utl_file.fclose(l_output);
END;
/
4.2. Call saved clob_to_file() to generate HTML file from CLOB content.
Execute the following anonymous block to generate the HTML file html_test_001.html in the * D:\tmp\html* directory :
DECLARE
v_clob CLOB;
BEGIN
-- 参考上面3.2步骤的使用,返回CLOB字段的HTML内容给到临时变量 v_clob
SELECT sql_to_html_xslt(q'{select * from scott_emp}')
INTO v_clob
FROM dual;
clob_to_file('HTML_DIR' -- 目录
,'html_test_001.html' -- 文件名
,v_clob); -- CLOB字段的值 v_clob
END;
The execution results are as follows:
4.3. View HTML files.
View the contents of the HTML file html_test_001.html in the * D:\tmp\html* directory :
Implementation of the second method:
1. Create a SQL file: D:\tmp\html.sql
set feedback off
set markup html on;
SET MARKUP HTML ON SPOOL ON PREFORMAT OFF ENTMAP ON HEAD "<TITLE>Department Report</TITLE> <STYLE type='text/css'> <!-- BODY {background: #FFFFC6} --> </STYLE>" BODY "TEXT='#FF00Ff'" TABLE "WIDTH='90%' BORDER='5'"
define data_path=D:\tmp\scripts
col ymd new_value v_ymd
select to_char(sysdate,'YYYYMMDDHH24MISS') ymd FROM dual;
spool D:\tmp\scripts\result_html_&&v_ymd..html
select * from scott.emp;
spool off
set markup html off
exit
2. Create a batch BAT program: D:\tmp\sql_to_html.bat
sqlplus log/1@tzq @D:\tmp\html.sql > D:\tmp\html.txt
exit;
3. Double-click the file (D:\tmp\sql_to_html.bat) to run the batch BAT program
The command line window will be called, and then it will flash, indicating that the BAT batch program has been executed.
Fourth, view the generated HTML file
So far, Oracle has generated the SQL query results into HTML webpage files, and the whole process is over. If you have any questions, please leave me a message, thank you!