溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Oracle通過Sqlplus結(jié)合Shell腳本方式生成Excel文件

發(fā)布時間:2020-08-07 16:39:05 來源:ITPUB博客 閱讀:312 作者:yhdmy 欄目:關(guān)系型數(shù)據(jù)庫
一、問題描述
       今天接到一個任務(wù),是要求每天通過SQL腳本生成excel文件,并且自動發(fā)送到相關(guān)人員郵箱。這個需求我還真是沒有做過,之前只做過通過SQL腳本生成為HTML網(wǎng)頁文件。于是乎,我又開始的學(xué)習(xí)過程,如今的Internet時代,解決問題就是easy,很快就找到了eygle的博客(使用SQL*PLUS,構(gòu)建完美excel或html輸出http://www.eygle.com/archives/2005/04/eoasqlplusieaae.html)。下面是學(xué)習(xí)過程中的實驗部分。

二、實驗
1.建立測試表

  1. SAM@dzwj > create table test1(id int,name varchar2(10),loc varchar2(30),hire_date date,email varchar2(20),department varchar2(20));
  2. Table created.

  3. SAM@dzwj > insert into test1 values (1,'sam1','beijing',sysdate,'sam1@oracle.com','it');
  4. 1 row created.
  5. SAM@dzwj > insert into test1 values (2,'sam2','beijing',sysdate,'sam2@oracle.com','it');
  6. 1 row created.
  7. SAM@dzwj > insert into test1 values (3,'sam3','beijing',sysdate,'sam3@oracle.com','it');
  8. 1 row created.
  9. SAM@dzwj > insert into test1 values (4,'sam4','beijing',sysdate,'sam4@oracle.com','it');
  10. 1 row created.
  11. SAM@dzwj > insert into test1 values (5,'sam5','beijing',sysdate,'sam5@oracle.com','it');
  12. 1 row created.

  13. SAM@dzwj > SAM@dzwj > commit;
  14. Commit complete.

  15. SAM@dzwj > select * from test1;

  16.         ID NAME LOC HIRE_DATE EMAIL DEPARTMENT
  17. ---------- ---------- ------------------------------ ------------------ -------------------- --------------------
  18.          1 sam1 beijing 28-NOV-17 sam1@oracle.com it
  19.          2 sam2 beijing 28-NOV-17 sam2@oracle.com it
  20.          3 sam3 beijing 28-NOV-17 sam3@oracle.com it
  21.          4 sam4 beijing 28-NOV-17 sam4@oracle.com it
  22.          5 sam5 beijing 28-NOV-17 sam5@oracle.com it


2.編輯 main.sql

  1. [oracle@testdb ~]$ cat main.sql
  2. set linesize 200 pagesize 10000
  3. set term off verify off feedback off
  4. set markup html on entmap on spool on preformat off
  5. alter session set NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS';
  6. spool /home/oracle/test1.xls
  7. @/home/oracle/get_tables.sql
  8. spool off
  9. exit

3.編輯 get_tables.sql

  1. [oracle@testdb ~]$ cat get_tables.sql
  2. select * from test1;

4.編輯執(zhí)行文件 collect.sh

  1. [oracle@testdb ~]$ cat collect.sh
  2. #!/bin/bash
  3. . /home/oracle/.bash_profile
  4. DATE=`date +%Y%m%d`
  5. sqlplus sam/oracle@dzwj @/home/oracle/main
  6. mv /home/oracle/test1.xls /home/oracle/test1_${DATE}.xls

5.給collect.sh 執(zhí)行權(quán)限

  1. [oracle@testdb ~]$ chmod u+x collect.sh

6.執(zhí)行

  1. [oracle@testdb ~]$ ./collect.sh

  2. SQL*Plus: Release 11.2.0.4.0 Production on Wed Nov 29 11:00:19 2017

  3. Copyright (c) 1982, 2013, Oracle. All rights reserved.


  4. Connected to:
  5. Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
  6. With the Partitioning, OLAP, Data Mining and Real Application Testing options

  7. Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
  8. With the Partitioning, OLAP, Data Mining and Real Application Testing options

7.驗證
將文件傳回到本地機器打開,得到想要的excel文件
Oracle通過Sqlplus結(jié)合Shell腳本方式生成Excel文件

三、總結(jié)
       生活在Internet時代真是件幸福的事,此次任務(wù)算是告一段落,但是當(dāng)中還是碰到一些小problems,比如一開始沒有加時間NLS_DATE_FORMAT變量的修改,導(dǎo)出的時間類型數(shù)據(jù)時沒有時間,只有年月日??偠灾?,多學(xué)習(xí),多實踐,沒錯的。向eygle大神致謝。  Where there is a will, there is a way.




向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI