Flexible combination of Linux/unix shell and SQL variable transfer, greatly improve the efficiency of the DBA, this article for Linux/unix Shell SQL passed between the variables given a few simple examples for reference.
Linux/unix cut with Sql,ramn please refer to: Sql,rman script in Linux/unix shell script
One, example
[Python]View PlainCopyprint?
- 1, the shell variable accepts the SQL return value the way one
- [Email protected]:~> more./retval.sh
- #!/bin/bash
- Retval= ' Sqlplus-silent Scott/tiger <<eof
- SET PAGESIZE 0 FEEDBACK off VERIFY off HEADING off ECHO off
- SELECT * from emp WHERE ename=' SCOTT ';
- EXIT;
- EOF '
- If [-Z "$RETVAL"]; Then
- echo "No rows returned from database"
- Exit 0
- Else
- Echo $RETVAL
- Fi
- [Email protected]:~> chmod u+x retval.sh
- [Email protected]:~>./retval.sh
- 7788 SCOTT ANALYST 7566 19-apr- 34171.88
- 2, the shell variable accepts the SQL return value the way two
- [Email protected]:~> more./retval_2.sh
- #!/bin/bash
- # Author:robinson Cheng
- # blog:http://blog.csdn.net/robinson_0612
- Sqlplus-s "Scott/tiger" <<eof
- SET PAGESIZE 0 FEEDBACK off VERIFY off HEADING off ECHO off
- Col CNT New_value v_cnt
- SELECT COUNT (*) CNT from emp WHERE deptno=10;
- Exit v_cnt
- Eof
- Value="$?"
- echo "Show rows for Deptno: $VALUE"
- 3. Pass the shell variable as a parameter to the SQL script
- [Email protected]:~> more./retval_3.sh
- #!/bin/bash
- v_empno="$"
- Sqlplus-s "Scott/tiger" <<eof
- SET PAGESIZE 0 FEEDBACK off VERIFY off HEADING off ECHO off
- SELECT ename from emp WHERE empno= $v _empno;
- Exit
- Eof
- Exit
- [Email protected]:~>./retval_3.sh 7788
- SCOTT
- Ext.: http://blog.csdn.net/leshami/article/details/7978179
Linux/unix passing variables between shell SQL