查询员工信息

-S:静默登录

[oracle@localhost shells]$ cat shell1.sh
#!/bin/bash #查询员工信息
sqlplus -S /nolog <<EOF
conn scott/scott
set feedback off
set linesize 300
set pagesize 100
col empno for 99999
col ename for a12
col mgr for 9999
col hiredate for a20
col comm for 9999
col deprno for 99999
select * from emp;
exit
EOF
[oracle@localhost shells]$ bash ./shell1.sh 

 EMPNO ENAME      JOB   MGR HIREDATE      SAL  COMM     DEPTNO
------ ------------ --------- ----- -------------------- ---------- ----- ----------
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7566 JONES MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7839 KING PRESIDENT 17-NOV-81 5000 10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7934 MILLER CLERK 7782 23-JAN-82 1300 10

使用代码块

[oracle@localhost shells]$ cat shell2.sh
#!/bin/bash
sqlplus -S scott/scott<<EOF
set feedback off
set serveroutput on
begin
dbms_output.put_line('hello world');
end;
/
exit
EOF
[oracle@localhost shells]$ bash ./shell2.sh
hello world

传入一个部门编号查询出该部门下的员工姓名

[oracle@localhost shells]$ cat shell3.sh
#!/bin/bash if [ $# -lt 1 ];then
echo 请传入部门编号
exit
fi
dno=$1
sqlplus -S scott/scott<<EOF
set feedback off
select ename from emp where deptno=${dno};
exit
EOF
[oracle@localhost shells]$ bash shell3.sh 10

ENAME
----------
CLARK
KING
MILLER

输入一个工作,根据工作查询员工的姓名

在sqlplus的EOF中,

单引号中的取变量符号和外面不同

它可以取到变量值

[oracle@localhost shells]$ cat shell4.sh
#!/bin/bash
if [ $# -lt 1 ];then
echo 请输入部门编号
exit
fi
sqlplus -S scott/scott<<EOF
set heading off
set feedback off
select ename from emp where deptno='$1';
exit
EOF
[oracle@localhost shells]$ bash shell4.sh
请输入部门编号
[oracle@localhost shells]$ bash shell4.sh 20 SMITH
JONES
SCOTT
ADAMS
FORD

将sql中的查询结果,传给shell脚本

传入一个部门编号,查询除部门的员工人数

并将sqlplus的结果传到shell脚本的变量中

[oracle@localhost shells]$ cat shell5.sh
#!/bin/bash if [ $# -lt 1 ];then
echo 请输入部门编号
exit
fi
dno=$1
num=`sqlplus -S scott/scott<<EOF
set heading off
set feedback off
select count(*) from emp where deptno=${dno};
exit
EOF`
echo $num
[oracle@localhost shells]$ bash shell5.sh 20
5
[oracle@localhost shells]$ bash shell5.sh 10
3

循环传入部门编号,查询除部门下员工的编号和姓名

[oracle@localhost shells]$ cat shell6.sh
#!/bin/bash if [ $# -lt 1 ];then
echo 请输入部门编号
exit
fi
dno=$1
informations=`sqlplus -S scott/scott<<EOF
set heading off
set feedback off
select ename from emp where deptno=${dno};
exit
EOF` for information in $informations
do
echo $information
done
[oracle@localhost shells]$ bash shell6.sh 20
SMITH
JONES
SCOTT
ADAMS
FORD
[oracle@localhost shells]$ cat shell7.sh
#!/bin/bash
if [ $# -lt 1 ];then
echo 请输入部门编号
exit
fi
dno=$1
names=`sqlplus -S scott/scott<<EOF
set heading off
set feedback off
select ename from emp where deptno=$dno;
exit
EOF` for((i=1;i<=100;i++))
do
name=`echo $names | cut -f $i -d ' '`
if [ -z $name ];then
break
fi
echo $name
done
[oracle@localhost shells]$ bash shell7.sh 10
CLARK
KING
MILLER

保存到文件

传入部门编号,查询部门下的员工编号和姓名

[oracle@localhost shells]$ cat shell8.sh
#!/bin/bash
if [ $# -lt 1 ];then
echo 请输入部门编号
exit
fi
dno=$1
sqlplus -S scott/scott > emp.txt<<EOF
set feedback off
select empno,job from emp where deptno=$dno;
exit
EOF
[oracle@localhost shells]$ bash shell8.sh 10
[oracle@localhost shells]$ cat emp.txt EMPNO JOB
---------- ---------
7782 MANAGER
7839 PRESIDENT
7934 CLERK

读取文件

[oracle@localhost shells]$ cat shell8.sh
#!/bin/bash
if [ $# -lt 1 ];then
echo 请输入部门编号
exit
fi
dno=$1
file=emp.txt
sqlplus -S scott/scott > $file<<EOF
set heading off
set feedback off
select empno,job from emp where deptno=$dno;
exit
EOF while read line
do
if [[ -z $line ]];then
continue
fi
mpno=`echo $line | cut -f 1 -d ' '`
name=`echo $line | cut -f 2 -d ' '`
echo "编号: ${mpno}, 工作: ${name}"
done < $file
rm -rf $file
[oracle@localhost shells]$ bash shell8.sh 20
编号: 7369, 工作: CLERK
编号: 7566, 工作: MANAGER
编号: 7788, 工作: ANALYST
编号: 7876, 工作: CLERK
编号: 7902, 工作: ANALYST

将数据导入到文件中

将emp表中的所有列的数据,导出到文件中,

列和列之间用逗号隔开

[oracle@localhost shells]$ cat shell9.sh
#!/bin/bash file=emp.txt
sqlplus -S scott/scott > $file<<EOF
set heading off
set feedback off
set pagesize 100
set linesize 300
select empno||','||ename||','||job||','||mgr||','||sal||','||comm||','||deptno from emp;
exit
EOF
[oracle@localhost shells]$ cat emp.txt 

7369,SMITH,CLERK,7902,800,,20
7499,ALLEN,SALESMAN,7698,1600,300,30
7521,WARD,SALESMAN,7698,1250,500,30
7566,JONES,MANAGER,7839,2975,,20
7654,MARTIN,SALESMAN,7698,1250,1400,30
7698,BLAKE,MANAGER,7839,2850,,30
7782,CLARK,MANAGER,7839,2450,,10
7788,SCOTT,ANALYST,7566,3000,,20
7839,KING,PRESIDENT,,5000,,10
7844,TURNER,SALESMAN,7698,1500,0,30
7876,ADAMS,CLERK,7788,1100,,20
7900,JAMES,CLERK,7698,950,,30
7902,FORD,ANALYST,7566,3000,,20
7934,MILLER,CLERK,7782,1300,,10

传入一个表名,查询该表的所有数据

[oracle@localhost shells]$ cat shell10.sh
#!/bin/bash
if [ $# -lt 1 ];then
echo 请输入一个表名
exit
fi
tab=$1
sqlplus -S scott/scott<<EOF
set feedback off
set pagesize 100
set linesize 300
select * from $tab;
exit
EOF
[oracle@localhost shells]$ bash shell10.sh dept

    DEPTNO DNAME    LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON

脚本备份数据库

将导出文件放到备份服务器或者目标服务器

[oracle@localhost ~]$ cat shell11.sh
#!/bin/bash
exp system/"oracle"@192.168.0.33:1521/orcl file='/home/oracle/data/exp.dump' log='/home/oracle/data/exp.log' owner=scott indexes=n
scp -r /home/oracle/data/exp.dump oracle@192.168.0.33:/home/oracle/dmp/
[oracle@localhost ~]$ bash shell11.sh
oracle@192.168.0.33's password:
[oracle@localhost ~]$ tree
.
├── data
│   ├── exp.dump
│   └── exp.log
├── dmp
│   └── exp.dump
└── shell11.sh

优化上面的语句

[oracle@localhost ~]$ cat shell12.sh
#!/bin/bash
#导出配置
dbuser=system
passwd=oracle
dbip=192.168.0.33
port=1521
sid=orcl
dumppath=/home/oracle/data
dt=`date "+%Y%m%d%H%M%S"`
logpath=/home/oracle/data
schema=scott
dumpfile=exp_${dt}.dump
logfile=exp_${dt}.log #备份服务器配置
desuser=oracle
desip=oracle
desip=192.168.0.33
dir=/home/oracle/dmp #将数据库中的数据导出
exp $dbuser/"$passwd"@$dbip:$port/$sid file=$dumppath/$dumpfile log=$dumppath/$logfile owner=$schema indexes=n #将导出文件放到备份服务器或者目标服务器
scp -r $dumppath/$dumpfile $desuser@$desip:$dir/
[oracle@localhost ~]$ bash shell12.sh
oracle@192.168.0.33's password:
[oracle@localhost ~]$ tree
.
├── data
│   ├── exp_20200307030903.dump
│   ├── exp_20200307030903.log
│   ├── exp.dump
│   └── exp.log
├── dmp
│   ├── exp_20200307030903.dump
│   └── exp.dump
├── shell11.sh
└── shell12.sh

将dept中的数据导出为dept.txt文件,并导入到另一张表中(dept_bak和dept表结构相同)

导出到dept.txt

sqlplus -S scott/scott > dept.txt<<EOF
set heading off
set feedback off
set pagesize 100
set linesize 300
select deptno||'|'||dname||'|'||loc from dept;
exit
EOF
[oracle@localhost ~]$ bash dept.sh
[oracle@localhost ~]$ cat dept.txt 10|ACCOUNTING|NEW YORK
20|RESEARCH|DALLAS
30|SALES|CHICAGO
40|OPERATIONS|BOSTON

在oracle中新建一张表

SQL> create table dept_bak as select * from dept where 1=0;

执行导入脚本

[oracle@localhost ~]$ cat impdept.sh
#!/bin/bash
while read line
do
if [[ -z $line ]];then
continue
fi
dno=`echo $line | cut -f 1 -d '|'`
name=`echo $line | cut -f 2 -d '|'`
l=`echo $line | cut -f 3 -d '|'`
sqlplus -S scott/scott > /dev/null<<EOF
insert into dept_bak(deptno,dname,loc) values($dno,'$name','$l');
exit
EOF
done < ./dept.txt
[oracle@localhost ~]$ bash impdept.sh
SQL> select * from dept_bak;

    DEPTNO DNAME    LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON

在shell脚本中调用sql语句的更多相关文章

  1. Shell脚本中执行sql语句操作mysql

    对于自动化运维,诸如备份恢复之类的,DBA经常需要将SQL语句封装到shell脚本.本文描述了在Linux环境下mysql数据库中,shell脚本下调用sql语句的几种方法,供大家参考.对于脚本输出的 ...

  2. Shell脚本中执行sql语句操作mysql的5种方法【转】

    对于自动化运维,诸如备份恢复之类的,DBA经常需要将SQL语句封装到shell脚本.本文描述了在Linux环境下mysql数据库中,shell脚本下调用sql语句的几种方法,供大家参考.对于脚本输出的 ...

  3. SHELL脚本中执行SQL语句操作MYSQL的5种方法

    对于自动化运维,诸如备份恢复之类的,DBA经常需要将SQL语句封装到shell脚本.本文描述了在Linux环境下mysql数据库中,shell脚本下调用sql语句的几种方法,供大家参考.对于脚本输出的 ...

  4. Linux/Unix shell 脚本中调用SQL,RMAN脚本

    Linux/Unix shell脚本中调用或执行SQL,RMAN 等为自动化作业以及多次反复执行提供了极大的便利,因此通过Linux/Unix shell来完成Oracle的相关工作,也是DBA必不可 ...

  5. shell 脚本中执行SQL语句 -e "..."

    /usr/local/mysql/bin/mysql -uroot -p123456 -e " use faygo source faygo.sql select * from devqui ...

  6. Linux脚本中调用SQL,RMAN脚本

    Linux/Unix shell脚本中调用或执行SQL,RMAN 等为自动化作业以及多次反复执行提供了极大的便利,因此通过Linux/Unix shell来完成Oracle的相关工作,也是DBA必不可 ...

  7. shell脚本中select循环语句用法

    shell脚本中select循环语句 1. 脚本中select的语法格式 select VAR in LIST do command1 command2 ... ... commandN done s ...

  8. Shell 脚本中调用另一个 Shell 脚本的三种方式

    主要以下有几种方式: Command Explanation fork 新开一个子 Shell 执行,子 Shell 可以从父 Shell 继承环境变量,但是子 Shell 中的环境变量不会带回给父 ...

  9. Shell脚本中调用另外一个脚本的方法

    (转载): 在Linux平台上开发,经常会在console(控制台)上执行另外一个脚本文件,经常用的方法有:./my.sh 或 source my.sh 或 . my.sh:这三种方法有什么不同呢?我 ...

随机推荐

  1. Random variable

    \(\underline{cdf:}\)cumulative distribution function \(F(x)=P(X \leq x)\) \(\underline{pmf:}\)probab ...

  2. 007.前端开发知识,前端基础CSS(2020-01-28)

    一.布局 一列固定宽度且居中 两列左窄右宽型 通栏平均分布型 1.一列固定宽度且居中布局<body> .top+.banner+.main+.footer 按Tab键,得到下框中代码 &l ...

  3. 有几张高度不一样的小图片,如何用html+css实现在同一行垂直居中对齐?

    方法一 :使用弹性布局 方法二 :CSS3 transform 属性 父级元素定位 子集元素加属性: position: absolute; top: 50%; transform: translat ...

  4. Apsara Clouder云计算专项技能认证:网站建设-简单动态网站搭建

    一.课程介绍 1.课程目标 了解静态网站和动态网站的区别 掌握动态网站的不同实现方式 在阿里云上如何搭建 wordPress网站以及 wordPress 网站的管理和优化 二.网站搭建的类型 1.网站 ...

  5. EXAM-2018-7-29

    EXAM-2018-7-29 未完成 [ ] H [ ] A D 莫名TLE 不在循环里写strlen()就行了 F 相减特判 水题 J 模拟一下就可以发现规律,o(n) K 每个数加一减一不变,用m ...

  6. MySQL的详细操作

    MySQL的详细操作 存储引擎 不同的数据应该有不同的处理机制 mysql存储引擎 Innodb:默认的存储引擎 查询速度较myisam慢 但是更安全,支持事务,行锁,外键由于以上的支持,数据更安全, ...

  7. Hibernate之Query.uniqueResult()结果为数值的注意事项

    在日常练习中使用Query.uniqueResult()获取查询总数量,想当然的把返回结果值直接强转成Integer类型,实现运行报错,具体代码如下: 控制台错误信息如下: 返回值为Long型,使用时 ...

  8. 1)session总结

    (1)session的由来: HTTP协议是一种无状态协议,服务器响应完之后就失去了与浏览器的联系,最早,Netscape将cookie引入浏览器,使得数据可以客户端跨页面交换,那么服务器是如何记住众 ...

  9. 转载-WebSocket协议解析

    现在,很多网站为了实现推送技术,所用的技术都是轮询.轮询是指在特定的时间间隔(如每一秒),由浏览器对服务器发起HTTP请求,然后由服务器返回数据给浏览器.由于HTTP协议是惰性的,只有客户端发起请求, ...

  10. PHP--修改数据库表前缀

    <?php // error_reporting(0); //设置好相关信息 $dbserver='localhost';//连接的服务器一般为localhost $dbname='jfpay' ...