在Oracle中,如何导出csv和html文件?
保存以下内容为spoolcsv.sql:
1set echo on 2set trimspool on 3set trimout on 4set linesize 4000 5set pagesize 0 6set sqlblanklines on 7set feedback off 8set serveroutput off 9set term off 10set echo off 13define data_path=E:\data 16col ymd new_value v_ymd 18select to_char(sysdate,'YYYYMMDDHH24MISS') ymd FROM dual; 20spool &data_path\result_&&v_ymd..csv 21SELECT substr(t.lie, 3) FROM (SELECT 0||','||'DEPTNO'||','||'DNAME'||','||'LOC' lie FROM dual a UNION SELECT replace(replace( 1||',' || REPLACE(DEPTNO,',','、')||',' || REPLACE(DNAME,',','、')||',' || REPLACE(LOC,',','、'),chr(10),''),chr(13),'') from SCOTT.DEPT) t; 22spool off 25exit 27---命令行 28--sqlplus lhr/lhr@orcl @f:\sql\spool\spool_csv.sql 31--SELECT fun_get_spool_string_lhr('SCOTT','EMP') FROM DUAL;
主要是使用:主要是使用 set markup html on
1SYS@PROD1> set markup 2SP2-0281: markup missing set option 3Usage: SET MARKUP HTML [ON|OFF] [HEAD text] [BODY text] [TABLE text] [ENTMAP {ON|OFF}] [SPOOL {ON|OFF}] [PRE[FORMAT] {ON|OFF}]
简单:
1set feedback off 2set markup html on; 3spool e:\data\salgrade.html 4select * from scott.emp; 5spool off 6set markup html off 7exit
漂亮的表格:
1set feedback off 2set markup html on; 3set markup html on spool on preformat off entmap on - 4head ' - <title> SCOTT.EMP表的数据</title> - <style type="text/css"> - body {font:11px Courier New,Helvetica,sans-serif; color:black; background:White;} - p {font:11px Courier New,Helvetica,sans-serif; color:black; background:White;} - table,tr,td {font:11px Courier New,Helvetica,sans-serif; color:Black; background:#FFFFCC; padding:0px 0px 0px 0px; margin:0px 0px 0px 0px;} - th {font:bold 11px Courier New,Helvetica,sans-serif; color:White; background:#0066cc; padding:0px 0px 0px 0px;} - h1 {font:bold 12pt Courier New,Helvetica,Geneva,sans-serif; color:White; background-color:White; border-bottom:1px solid #cccc99; margin-top:0pt; margin-bottom:0pt; padding:0px 0px 0px 0px;} - h2 {font:bold 11pt Courier New,Helvetica,Geneva,sans-serif; color:White; background-color:White; margin-top:4pt; margin-bottom:0pt;} - a {font:11px Courier New,Helvetica,sans-serif; color:#663300; margin-top:0pt; margin-bottom:0pt; vertical-align:top;} - a.link {font:11px Courier New,Helvetica,sans-serif; color:#663300; margin-top:0pt; margin-bottom:0pt; vertical-align:top;} - a.noLink {font:11px Courier New,Helvetica,sans-serif; color:#663300; text-decoration: none; margin-top:0pt; margin-bottom:0pt; vertical-align:top;} - a.noLinkBlue {font:11px Courier New,Helvetica,sans-serif; color:#0000ff; text-decoration: none; margin-top:0pt; margin-bottom:0pt; vertical-align:top;} - a.noLinkDarkBlue {font:11px Courier New,Helvetica,sans-serif; color:#000099; text-decoration: none; margin-top:0pt; margin-bottom:0pt; vertical-align:top;} - a.noLinkRed {font:11px Courier New,Helvetica,sans-serif; color:#ff0000; text-decoration: none; margin-top:0pt; margin-bottom:0pt; vertical-align:top;} - a.noLinkDarkRed {font:11px Courier New,Helvetica,sans-serif; color:#990000; text-decoration: none; margin-top:0pt; margin-bottom:0pt; vertical-align:top;} - a.info:hover {background:#eee;color:#000000; position:relative;} - a.info span {display: none; } - a.info:hover span {font-size:11px!important; color:#000000; display:block;position:absolute;top:30px;left:40px;width:150px;border:1px solid #ff0000; background:#FFFF00; padding:1px 1px;text-align:left;word-wrap: break-word; white-space: pre-wrap; white-space: -moz-pre-wrap} - </style>' - 24body 'BGCOLOR="#C0C0C0"' 27SET MARKUP html TABLE 'WIDTH="100%" border="1" summary="Script output" cellspacing="0px" style="border-collapse:collapse;" ' 29define data_path=E:\data 31col ymd new_value v_ymd 33select to_char(sysdate,'YYYYMMDDHH24MISS') ymd FROM dual; 35spool &data_path\result_html_&&v_ymd..html 36select * from scott.emp; 37spool off 38set markup html off 39exit
再例如我的健康检查脚本也是通过这种方式导出的:
& 说明:
有关本小节的更多内容可以参考我的BLOG:http://blog.itpub.net/26736162/viewspace-2149543/
本文选自《Oracle程序员面试笔试宝典》,作者:李华荣。
详细内容可以添加麦老师微信或QQ私聊。
● 本文作者:小麦苗,只专注于数据库的技术,更注重技术的运用
● 作者博客地址:http://blog.itpub.net/26736162/abstract/1/
● 本系列题目来源于作者的学习笔记,部分整理自网络,若有侵权或不当之处还请谅解
● 版权所有,欢迎分享本文,转载请保留出处
● QQ:646634621 QQ群:618766405
● 提供OCP、OCM和高可用部分最实用的技能培训
● 题目解答若有不当之处,还望各位朋友批评指正,共同进步
长按下图识别二维码或微信扫描下图二维码来关注小麦苗的微信公众号:xiaomaimiaolhr,学习最实用的数据库技术。
本文分享自微信公众号 - DB宝(lhrdba)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。