Shell脚本调用SQL文格式

1. 定义需要执行的SQL文,以及需要输出文件

OUTFILE=\${DATADIR}/\${FILENAME}

SQLFILE=\${DATADIR}/check_tradedetail.sql

2. 设置需要执行的SQL文

echo -n "

select tr.id || ',' || m.merchant_code || ',' || m.merchant_name || ',' || to_char(tr.trade_start_time, 'yyyy-mm-dd hh24:mi:ss')

|| ',' || tt.name || ',' || tr.bank_code || ',' ||  tr.fld3 || ',' ||

tr.trade_sum || ',' ||

case when tr.status = 0 then '待处理'

when tr.status = 1 then '处理中'

when tr.status = 2 then '成功'

when tr.status = 3 then '失败'

when tr.status = 4 then '待确认' end

from tvpay2.trade_record tr join tvpay2.merchant m on tr.provider_id = m.id

join tvpay2.trade_type tt on tr.trade_code = tt.code

where to_char(tr.fiscal_date, 'yyyyMMdd') = ${date} order by tr.trade_start_time;

" > $SQLFILE

echo -n "
select ''''||a.merchant_code || ',' || a.merchant_name || ',''' || a.id  
|| ','  || (a.balance + nvl(b.acc, 0))  || ','  || (a.withdrawable_balance + nvl(b.acc, 0)) || ',' ||  a.frozen_balance   from
(select tu.merchant_code, tu.merchant_name, a.* from tvpay2.account a join tvpay2.merchant_account_association ua
on a.id = ua.account_id join tvpay2.merchant tu on ua.merchant_id = tu.id ) a
left join
(select v.account_id, sum(v.credited - v.debited) as acc from tvpay2.voucher v  
where v.create_time <= to_date('\$input_datetime', 'yyyymmdd hh24:mi:ss') group by v.account_id)  b
on a.id = b.account_id where a.balance + nvl(b.acc, 0) > 0;
" > \$SQLFILE1

#备注 ||表示分隔符  ‘,’用于分隔字段使用 其中‘’‘’为了在第一个字段中添加'号,另外‘、'\"’可以添加“号,这个可以用来解决文本文件,转换成csv格式时,数字错乱问题。

3. 输出文件格式设置及其执行SQL文

##set colsep' ';        //域输出分隔符

##set numwidth 25 ;     //输出number类型域长度,缺省为10

##set linesize 300;     //输出一行字符个数,缺省为80

##set pagesize 0;       //输出每页行数,缺省为24,为了避免分页,可设定为0,设为0后无域标题。

##set echo off;                //显示start启动的脚本中的每个sql命令,缺省为on

##set newpage none;          //页与页之间的分隔符

##set termout off;      //显示脚本中的命令的执行结果,缺省为on

##set trimout on;              //去除标准输出每行的拖尾空格,缺省为off

##set trimspool on;     //去除重定向(spool)输出每行的拖尾空格,缺省为off

##set serveroutput on;  //设置允许显示输出类似dbms_output

##set timing on;        //设置显示“已用时间:XXXX”

##set verify off;       //可以关闭和打开提示确认信息old 1和new 1的显示.

##set heading off;    //输出域标题,缺省为on

##set feedback off;     //回显本次sql命令处理的记录条数,缺省为on

##alter session set nls_date_format='YYYY-MM-DD_HH24:MI:SS';

sqlplus /nolog <<!

conn user/password@ORCL

set numwidth 25

set linesize 300

set pagesize 0

set echo off

set heading off

set newpage none

set termout off

set trimout on

set trimspool on

set serveroutput off

set timing off

set verify off

set feedback off

spool ${OUTFILE};

@${SQLFILE};

spool off;

!

4.格式化输出文件

sed -i '/SQL/d' \$OUTFILE1
sed -i '/---/d' \$OUTFILE1
sed -i 's/\ \ //g' \$OUTFILE1
sed -i '/^\$/d' \$OUTFILE1

5.将文本格式转换为csv格式

iconv -f "utf-8" -t "gbk" \${OUTFILE1} > OUTFILE1.csv

Shell脚本调用SQL文格式的更多相关文章

  1. 基于sparksql调用shell脚本运行SQL

    [Author]: kwu 基于sparksql调用shell脚本运行SQL,sparksql提供了类似hive中的 -e  , -f ,-i的选项 1.定时调用脚本 #!/bin/sh # uplo ...

  2. shell脚本调用spark-sql

    为了更方便的查询并产生报表, 需要使用shell脚本调用spark-sql spark/bin/spark-sql --master spark://host:7077 -f ${SQL_FILE} ...

  3. shell脚本调用C语言之字符串切分之strtok函数

    今天上午在写一个需求,要求的比较急,要求当天完成,我大致分析了一下,可以采用从shell脚本中插入一连串的日期,通过调用proc生成的可执行文件,将日期传入后台数据库,在数据库中进行计算.需要切分日期 ...

  4. shell脚本调用C语言之字符串切分函数——strtok

    今天上午在写一个需求,要求的比较急,要求当天完成,我大致分析了一下,可以采用从shell脚本中插入一连串的日期,通过调用proc生成的可执行文件,将日期传入后台数据库,在数据库中进行计算.需要切分日期 ...

  5. Shell脚本调用ftp上传文件

    Shell脚本调用ftp上传文件 1.脚本如下 ftp -n<<! open x.x.x.x ###x.x.x.x为ftp地址 user username password ###user ...

  6. Miniconda 安装 & Pip module 安装 & Shell 脚本调用 Miniconda 虚拟环境手册(实战项目应用)

    (实战项目应用) 1. 下载Miniconda 两个安装方式: 方式1:wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Min ...

  7. MySql创建函数与过程,触发器, shell脚本与sql的相互调用。

    一:函数 1:创建数据库和表deptartment, mysql> use DBSC; Database changed mysql), ), )); Query OK, rows affect ...

  8. linux下crontab定时执行shell脚本调用oracle 存储过程

    问题:脚本内调用存储过程,脚本直接执行没问题,使用crontab 执行脚本存储过程未执行 原因:缺少oracle环境变量 解决:在shell脚本里添加oracle的环境变量 #!/bin/sh PAT ...

  9. shell脚本调用python脚本的路径问题

    脚本的相互调用中,只有在同一级目录下才可以使用__file__参数去获取路径名,(在shell里使用pwd也同样),否则,使用的就是主文件(最开始运行的脚本程序)的所在位置,是错误路径:一定要注意当期 ...

随机推荐

  1. [Xcode 实际操作]二、视图与手势-(1)UIView视图的基本使用

    目录:[Swift]Xcode实际操作 本文将演示在视图控制器的根视图里添加两个视图对象. import UIKit class ViewController: UIViewController { ...

  2. ubuntu 14 安装XML::Simple 模块

    最近需要用到perl 来解析xml 文件,从网上搜索了一下,大部分都建议使用XML::Simple 模块来解析,这里记录一下安装过程 方法一: 直接使用CPAN 来安装模块 $ perl -MCPAN ...

  3. bootstrapValidator 常用的验证

    $("#表单ID").bootstrapValidator({ message: 'This value is not valid', excluded: [':disabled' ...

  4. 自定义UIButton 实现图片和文字 之间距离和不同样式

    喜欢交朋友的加:微信号 dwjluck2013 1.UIButton+ImageTitleSpace.h #import <UIKit/UIKit.h> // 定义一个枚举(包含了四种类型 ...

  5. JS的this原理

    转载阮一峰博客: www.ruanyifeng.com/blog/2018/06/javascript-this.html 一.问题的由来   学懂 JavaScript 语言,一个标志就是理解下面两 ...

  6. [poj 2104] K-th Number【主席树】

    传送门:http://poj.org/problem?id=2104 保存模版. #include <cstdio> #include <algorithm> #include ...

  7. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) A

    Description There are literally dozens of snooker competitions held each year, and team Jinotega tri ...

  8. ZOJ Course Selection System DP

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5565 Course Selection System Time ...

  9. MySql下载地址

    因为下载mysql需要注册,很麻烦,记录下下载地址: My sql 5.1.71 http://cdn.mysql.com/Downloads/MySQL-5.1/mysql-5.1.71-win32 ...

  10. React 实践记录 02 Flux introduction

    Introduction 本文组成: React 官方文档翻译 相关实践心得. 内容上是Flux的介绍,例子将会在以后写出. 一旦稍微多了解一点React,很难避免听到Flux这个名词. Flux是一 ...