1

I have 2 scripts - one shell and one sql.

My shell script is similar to this:

export nbr=&1 

runsql script_name.sql

Im trying to pass a parameter for nbr while running the script.

The corresponding sql script is as such:

insert into table1

select * from table2 

where year='&1'

I get the error as below:

"enter value for year: old 22: where year='$1') 
                       new 22: where year='commit')"
6
  • $nbr is used nowhere ... Commented Sep 26, 2022 at 21:06
  • i put nbr as a place holder to pass a parameter "year' that is used in the sql script. is that causing the error? Commented Sep 26, 2022 at 21:21
  • 1
    Probably a duplicate of sqlplus: get parameters of file.sql execution Commented Sep 26, 2022 at 21:45
  • You haven't shown commit being passed, so is that coming from whatever runsql is - either another script or a function in this script? Or on the command line when you call your shell script? The output is (partly - where is the prompt coming from?) what you see with set verify on, but you've shown $1 not &1. So this doesn't seem to be what you are really running. It would really help if you showed your actual code, not something significantly different. Commented Sep 26, 2022 at 22:02
  • 1
    You need to provide all your code, so we don't need to guess it. Show your runsql Commented Sep 27, 2022 at 2:14

1 Answer 1

0

I'll show how to deal with it easily with an example based on my own scripts. In this script I define a simple function run_sql(DESCR, SCRIPT, DATABASES), where

  • DESCR - short description
  • SCRIPT - sqlplus run arguments, ie script name and its parameters
  • DATABASES - list of dbname defined above on which you want to run it

Then we can easily use it like this:

run_sql "1st script" "@sql1.sql param1" db1 db2
run_sql "2nd script" "@sql2.sql param1 param2 param3" db1 db2 db3

Here we execute:

  • "sql1.sql" with 1 argument on db1 and db2
  • "sql2.sql" with 3 arguments on db1,db2 and db3

And we save all output into own log files.

Full example with test output: https://gist.github.com/xtender/465951befeed7f0ae1a3fe112dcd7fe4

Simple script test.sh:

#!/bin/bash

# Here you can define your db connection strings:
db1=xtender/pass@PDB19C_11
db2=xtender/pass@PDB19C_11
db3=xtender/pass@PDB19C_11
#####################################################################
# functions:
# Function syntax: run_sql(DESCR, SCRIPT, DATABASES)
# where
#   DESCR - short description
#   SCRIPT - sqlplus run arguments, ie script name and its parameters
#   DATABASES - list of dbname defined above on which you want to run it
run_sql(){
  local DESCR="$1"; shift
  local SCRIPT="$1"; shift
  local databases=("$@")
  echo =================================================
  echo =   $DESCR
  echo = Going to execute $SCRIPT...
  read -a res -p "Enter 'skip' to skip this step or press Enter to execute: "
  if [[ $res = "skip" ]]
  then
    echo Skipping $SCRIPT...
  else
    echo Executing $SCRIPT...
    for db in "${databases[@]}"
        do
          local cur=${!db}
          echo Executing $SCRIPT on $db - $cur...
          sqlplus -L -S ${cur} $SCRIPT >>log-$db.log 2>&1
          echo Done.
        done
    echo =================================================
  fi
}
#####################################################################


# Here we execute a script "sql1.sql" with one argument "param1" on db1 and db2:
run_sql "1st script" "@sql1.sql param1" db1 db2

# Here we execute a script "sql2.sql" with 3 arguments on db1,db2 and db3:
run_sql "2nd script" "@sql2.sql param1 param2 param3" db1 db2 db3
echo ============================================
echo === Done
echo ============================================

Then we can create sql scripts, for example sql1.sql and sql2.sql: sql1.sql requires one argument and sql2.sql - 3 arguments:

sql1.sql:

select '&1' as output from dual;
exit;

sql2.sql:

select
  '&1' out1,
  '&2' out2,
  '&3' out3
from dual;
exit;

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.