Linux:sqlplus


[oracle@hb shell_test]$ cat echo_time #!/bin/sh 一.最简单的调用sqlplus sqlplus -S "sys/unimas as sysdba" << ! select to_char(sysdate,'yyyy-mm-dd') today from dual; exit; ! [oracle@hb shell_test]$ ./echo_time

运行结果:

TODAY
----------
2011-03-21

-S 是silent mode,不输出类似“SQL>”,连接数据库,关闭数据库之类的信息。

EOF

eof可以是任何字符串 比如"laldf"那么当你输入单独一行laldf时"shell认为输入结束,但是必须表示块开始必须使用<<;
开始和结束要匹配这个符号“<<”后面的内容
举例子:

[oracle@hb shell_test]$ sqlplus -s "sys/unimas as sysdba" << abc
> select to_char(sysdate,'yyyy-mm-dd') today from dual;
> exit;
> abc

运行结果

TODAY
----------
2011-03-21

二.sqlplus的结果传递给shell的方法一

[oracle@hb shell_test]$ cat test2.sh 
#!/bin/bash
VALUE=`sqlplus -S "test/unimas" << !
set heading off
set feedback off
set pagesize 0
set verify off
set echo off
select to_char(sysdate,'yyyy-mm-dd') today from dual;
exit
!`
echo $VALUE
if [ -n "$VALUE" ]; then
echo "The rows is $VALUE"
exit 0
else
echo "There is no row"
fi

三.sqlplus的结果传递给shell的方法二

[oracle@hb shell_test]$ cat test1.sh 
#!/bin/bash
sqlplus -S "test/unimas" << !
set heading off
set feedback off
set pagesize 0
set verify off
set echo off
col coun new_value v_coun
select count(*) coun from lesson;
exit v_coun
!
VALUE="$?"
echo "show row:$VALUE"
col coun new_value v_coun v_coun为number类型。因为exit 只能返回数值类型。

四.把shell参数传递给sqlplu

#!/bin/bash
t_id="$1"
sqlplus -S "test/unimas" << !
set heading off
set feedback off
set pagesize 0
set verify off
set echo off
select teachername from teacher where id=$t_id;
exit
!

五.sqlplus的结果存储在文件中

#!/bin/sh
sqlplus -S "test/unimas"<<EOF
set heading off
set feedback off
set pagesize 0
set verify off
set echo off
spool spool_file
SELECT * from teacher;
spool off
exit;
EOF

转载于:https://www.cnblogs.com/xwb583312435/p/9002334.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值