Sqlplus Example of exporting Oracle query results

Source: Internet
Author: User
Tags sqlplus

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"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.