shell脚本之数据库导数导出指定格式

shell脚本之数据库导数导出指定格式

功能描述
需求如下:
需要从数据库导出指定格式的数据,如:以逗号分隔,或是以|分割的内容。

export.sh

#!/usr/bin/bash

datefile=$1
work_path=$2

start_time=`date +%Y%m%d%H%M%S`
#work_path=/home/oracle/fk_data
DbCon=rdsadmin/rds@orcl
file_date=`date +%Y%m%d`
filename=${work_path}/"$datefile"_${file_date}.txt

sqlinfo="select * from $datefile;"
echo "执行的sql: $sqlinfo"

echo "----------------  start to exec job   $start_time "

echo "----------------  connect to $DbCon and try to exec sql..."
 
 
sqlplus -S $DbCon<< EndSql
set line 1000
set pagesize 0
set feedback off
set heading off
set trimspool on
set trims on
set echo off
set colsep '|'
--set termout off
spool $filename
$sqlinfo
spool off
exit
EndSql
#去除文件开头的空格及中间的空白字符
sed -i 's/ //g' ${work_path}/"$datefile"_${file_date}.txt
end_time=`date +%Y%m%d%H%M%S`
echo "----------------  cmd end , please check te result $end_time"

逻辑描述:接收两个参数,第一个参数是要导出的表名,第二个参数是导出数据文件的名称,这里以日期作为标识。最后生成的文件有一个去掉空白字符的功能。

备注:上述脚本中的sqlinfo如
select * from ts_maodou_data t;

MD5加密函数

--MD5
create or replace function MD5(message in varchar2)
return varchar2
is
retval varchar2(32);

begin

       retval:=utl_raw.cast_to_raw(DBMS_OBFUSCATION_TOOLKIT.MD5(INPUT_STRING => message));
       
       return retval;  

end MD5;

备注:如果导出的数据需要md5加密,则需要在数据库创建此函数。

执行命令

sh ./export.sh 表名 /home/oracle/maodou/oracledata

其它内容

–查看数据库字符集编码
select userenv(‘language’) from dual;
–执行脚本的服务器指定字符编码
NLS_LANG=“SIMPLIFIED CHINESE_CHINA.AL32UTF8”
–使NLS_LANG生效
export NLS_LANG

猜你喜欢

转载自blog.csdn.net/weixin_38717886/article/details/115271232