1, create a query Oracle SQL script file, named Cx.sql, which defines three variables
Last_date Yesterday's time
Today: the time of day
file_name: Exported filename
&1, &2, &3 represent the three arguments that are passed to the shell script that is used later to invoke Cx.sql this script
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, use shell script to invoke this SQL script
There are three parameters passed to Cx.sql in the import data, which are yesterday's date, today's date, the file name of the exported data, and send the exported file to the user via 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 "title"-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 "Body"