Passing variables between Linux/Unix shell SQL statements

Source: Internet
Author: User

The flexible combination of Linux/Unix Shell and SQL variable transmission greatly improves DBA efficiency, this article provides a few simple examples of passing variables between Linux/Unix shell SQL statements for your reference.

Call SQL in Linux/Unix. For details about RAMN, refer to SQL and RMAN scripts in Linux/Unix shell scripts.

I. Example

1. method 1 for accepting SQL return values for shell Variables
Oracle @ SZDB: ~> 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

Oracle @ SZDB: ~> Chmod u + x retval. sh
Oracle @ SZDB: ~> ./Retval. sh
7788 scott analyst 7566 19-APR-87 34171.88 20

2. method 2 for shell variables to accept SQL return values
Oracle @ SZDB: ~> More./retval_2.sh
#! /Bin/bash
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 10: $ VALUE"

3. Pass the shell variable as a parameter to the SQL script
Oracle @ SZDB: ~> More./retval_3.sh
#! /Bin/bash
V_empno = "$1"
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

Oracle @ SZDB: ~> ./Retval_3.sh 7788.
SCOTT

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.