sqlplus匯出oracle查詢結果的例子

來源:互聯網
上載者:User

1、建立查詢oracle的sql指令檔,名為cx.sql,其中定義了三個變數

LAST_DATE昨天的時間
TODAY:今天的時間
FILE_NAME:匯出的檔案名稱
&1、&2、&3 表示後面使用的shell指令碼調用cx.sql這個指令碼時向它傳遞的三個參數


SET NEWPAGE 0
SET SPACE 0
SET LINESIZE 80
SET PAGESIZE 0
SET ECHO OFF
SET FEEDBACK OFF
SET VERIFY OFF
SET HEADING OFF
SET MARKUP HTML OFF SPOOL OFF
define LAST_DATE='&1'
define TODAY='&2'
define FILE_NAME='&3'
 
set markup html on;
spool &3

#查詢語句

select ****
 from table_name
where
b.updatedate>=to_date('&LAST_DATE 9:00','yyyy-mm-dd hh24:mi') and b.updatedate<to_date('&TODAY 9:00','yyyy-mm-dd hh24:mi')
and a.isquery=1 and a.currintfee>=0 and b.currintegral>=5200 and b.currintegral<=30000
order by fee;
spool off;
exit

2、使用shell指令碼調用該sql指令碼

在匯入資料這裡向cx.sql傳遞了三個參數,分別是昨天的日期、今天的日期、匯出資料的檔案名稱,並通過sendEmail將匯出檔案通過附件發送給使用者


#!/bin/bash
#設定環境變數
export PATH
export EDITOR=vim
export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1
export ORACLE_SID=orcl
export ORACLE_UNQNAME=orcl
export ORACLE_HOSTNAME=db2
export PATH=$PATH:/$ORACLE_HOME/bin
export NLS_LANG="SIMPLIFIED CHINESE_CHINA.UTF8"
alias sqlplus='/usr/local/rlwrap/bin/rlwrap sqlplus'
alias rman='/usr/local/rlwrap/bin/rlwrap rman'
export  NLS_DATE_FORMAT="YYYY-MM-DD HH24:MI:SS"
#設定三個參數
LAST_DATE=`date -d"yesterday" +%Y-%m-%d`
TODAY=`date -d "today" +"%Y-%m-%d"`
FILE_NAME="${LAST_DATE}_${TODAY}.html"
 
#匯出資料
sqlplus oracle_user/oracle_pass @"cx.sql" "$LAST_DATE" "$TODAY" "$FILE_NAME"
echo "query is finish"
 
#打包資料
tar -czvf ${LAST_DATE}_${TODAY}.tar.gz ${LAST_DATE}_${TODAY}.html
rm -rf ${LAST_DATE}_${TODAY}.html
 
#發送郵件
/usr/local/bin/sendEmail -f send@test.com -t receive@test.com \
 -s smtp.exmail.qq.com -u "標題" -o message-content-type=html \
 -o message-charset=utf8 -a /home/oracle/jfcx/${LAST_DATE}_${TODAY}.tar.gz -xu send@test.com \
 -xp send_pass -m "本文"

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.