Linux/unix shell arguments to SQL scripts

Source: Internet
Author: User
Tags exit sqlplus linux

In the process of database operation, the Shell script provides great convenience for operation and maintenance. A shell script parameter passed as a variable to SQL and a SQL script is one of the scenarios that DBAs often encounter. This article mainly discusses how to pass the parameters of the shell script to the SQL script and execute the SQL query.

1. Execute scripts and pass parameters when starting Sqlplus

robin@szdb:~/dba_scripts/custom/awr> more tmp.sh

#!/bin/bash

# ----------------------------------------------

# Set Environment here

# Author:robinson Cheng

# ----------------------------------------------

If [f ~/.bash_profile]; Then

. ~/.bash_profile

Fi

If [-Z "${1}"] | | [-Z "${2}"] | | [-Z "${3}"]; then

echo "Usage:"

echo "' basename $ ' <ORACLE_SID> <begin_dat> <end_date>"

Read-p "Please input begin ORACLE_SID:" Oracle_sid

Read-p "Please input begin date and time (e.g. yyyymmddhh24):" Begin_date

Read-p "Please input end date and time (e.g. yyyymmddhh24):" End_date

Else

Oracle_sid=${1}

BEGIN_DATE=${2}

END_DATE=${3}

Fi

Export Oracle_sid begin_date end_date

#Method 1:pass the parameter to script directly after script name

Sqlplus-s gx_adm/gx_adm @/users/robin/dba_scripts/custom/awr/tmp.sql $begin _date $end _date

Exit

robin@szdb:~/dba_scripts/custom/awr> more Tmp.sql

SELECT snap_id, dbid, Snap_level

From Dba_hist_snapshot

WHERE to_char (begin_interval_time, ' yyyymmddhh24 ') = ' &1 '

and To_char (end_interval_time, ' yyyymmddhh24 ') = ' &2 ';

Exit

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.