oracle-sql脚本导出EXCEL数据

时间:2023-03-09 06:40:01
oracle-sql脚本导出EXCEL数据

在数据库中,经常有业务人员提出需求导出数据库中的业务数据,而且是每天、每周或每月定时导出。为了方便,可将sql查询的脚本 通过下面脚本来导出EXCEL数据。

1.将查询sql脚本(AAA.sql)放到 相关目录如:/home/oracle/tongji-scripts/sql/AAA.sql

2.# vim  sql-excel.sh

source /home/oracle/.bash_profile
export NLS_LANG=AMERICAN_AMERICA.ZHS16GBK
export LANG=zh_CN.GBK

function EXESQL(){
sqlplus -s test/"123456" <<EOF
set linesize 200
set term off verify off feedback off pagesize 999
set markup html on entmap ON spool on preformat off
spool /home/oracle/tongji-scripts/result/$1_`date +%y%m%d`.xls
@/home/oracle/tongji-scripts/sql/$1;
spool off
exit;
EOF
}

for file in `ls /home/oracle/tongji-scripts/sql/`
do
EXESQL ${file}
done

3.添加定时任务: 00  03  *  *   *  /home/oracle/sql-excel.sh

每天定时3点会将 /home/oracle/tongji-scripts/sql/下面的多个sql脚本导出 生成EXCEL文件 存放到 /home/oracle/tongji-scripts/result/目录下。