欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

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

一、問(wèn)題描述
       今天接到一個(gè)任務(wù),是要求每天通過(guò)SQL腳本生成excel文件,并且自動(dòng)發(fā)送到相關(guān)人員郵箱。這個(gè)需求我還真是沒(méi)有做過(guò),之前只做過(guò)通過(guò)SQL腳本生成為HTML網(wǎng)頁(yè)文件。于是乎,我又開(kāi)始的學(xué)習(xí)過(guò)程,如今的Internet時(shí)代,解決問(wèn)題就是easy,很快就找到了eygle的博客(使用SQL*PLUS,構(gòu)建完美excel或html輸出http://www.eygle.com/archives/2005/04/eoasqlplusieaae.html)。下面是學(xué)習(xí)過(guò)程中的實(shí)驗(yàn)部分。

二、實(shí)驗(yàn)
1.建立測(cè)試表

  1. SAM@dzwj> create table test1(id int,namevarchar2(10),locvarchar2(30),hire_datedate,emailvarchar2(20),departmentvarchar2(20));
  2. Table created.

  3. SAM@dzwj> insert into test1values (1,'sam1','beijing',sysdate,'sam1@oracle.com','it');
  4. 1row created.
  5. SAM@dzwj> insert into test1values (2,'sam2','beijing',sysdate,'sam2@oracle.com','it');
  6. 1row created.
  7. SAM@dzwj> insert into test1values (3,'sam3','beijing',sysdate,'sam3@oracle.com','it');
  8. 1row created.
  9. SAM@dzwj> insert into test1values (4,'sam4','beijing',sysdate,'sam4@oracle.com','it');
  10. 1row created.
  11. SAM@dzwj> insert into test1values (5,'sam5','beijing',sysdate,'sam5@oracle.com','it');
  12. 1row 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 200pagesize 10000
  3. set termoff verifyoff feedbackoff
  4. set markup htmlon entmapon 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 Productionon Wed Nov 29 11:00:19 2017

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


  4. Connectedto:
  5. OracleDatabase 11g Enterprise EditionRelease 11.2.0.4.0- 64bit Production
  6. With the Partitioning, OLAP, Data Miningand Real Application Testing options

  7. Disconnectedfrom OracleDatabase 11g Enterprise EditionRelease 11.2.0.4.0- 64bit Production
  8. With the Partitioning, OLAP, Data Miningand Real Application Testing options

7.驗(yàn)證
將文件傳回到本地機(jī)器打開(kāi),得到想要的excel文件
Oracle通過(guò)Sqlplus結(jié)合Shell腳本方式生成Excel文件

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




分享名稱(chēng):Oracle通過(guò)Sqlplus結(jié)合Shell腳本方式生成Excel文件
文章轉(zhuǎn)載:http://www.chinadenli.net/article40/peggho.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁(yè)設(shè)計(jì)公司Google外貿(mào)網(wǎng)站建設(shè)定制開(kāi)發(fā)網(wǎng)站導(dǎo)航App設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

微信小程序開(kāi)發(fā)