shell语言 调用数据库

这篇博客展示了如何使用Shell脚本调用数据库执行SQL查询,并通过sed命令对查询结果进行格式化处理。脚本包括对物理库和内存库的循环调用,将查询结果追加到文件中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#!/usr/bin/ksh
>33333.txt
sqlplus -s 数据库连接串<<!>>33333.txt
set colsep |
set pagesize 0
set linesize 1000
set serveroutput on size 10000 
set pagesize 0
set head off
select '#############' a  from dual;
select * from  usr_134.bc_ist_record where result_code='118185044';
!

输出结果 进行规整


sed "s/  *//g" 33333.txt

sed "s/\t//g"  33333.txt

 

sed  -i '1i\date     be_id  domain  proc_type   error_code  count(*)' /onip/app/billsharedb/lyy/error.txt


sed "s/  *//g" | sed "s/\t//g" >> 1234.txt

循环调用物理库

#!/usr/bin/ksh


sql_mod="set line 300\n set head off\n set feed off\n set pagesize 0\n set colsep \"|\" \n col acct_id for 9999999999999999999\n  alter session set nls_date_format='yyyymmddhh24miss'; \n"
>1234.txt

echo "cust_id|be_id|tp_cust_key">>1234.txt

cat link1.cfg|grep "usrdb"|awk -F "|" '{print $2,$3}'|while read conn_str pdbuser
do

      sql_str="select  t.cust_id,t.be_id,t.tp_cust_key from ${pdbuser}.v_bc_customer t  where t.cust_code in ('111000002641000','11000002458000','11000001493001','11000002502000');"
      echo -e "${sql_mod}${sql_str}" | sqlplus -s $conn_str |sed "s/  *//g" | sed "s/\t//g" >> 1234.txt

done
循环调用内存库

 

#!/bin/sh
>1111.txt
tdbyd=$(date -d"`date +%y%m01` last day" +%Y%m%d160000)

echo "----数量----">>1111.txt

cat link2.cfg|grep "cbpmdb"|awk -F "|" '{print $2,$3}'|while read conn_str mdbuser

do

sql_rentevent="select count(*) from $mdbuser.e_sub_apn_status where STATUS_TYPE=1 and EFF_DATE>=to_date('${tdbyd}','YYYYMMDDHH24MISS');"

gmsql ${conn_str} "${sql_rentevent}" |sed 's/ */ /g'| head -n -4 | tail -n +6 >>1111.txt

done

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天行健自强不息的码农

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值