简体   繁体   中英

Needs to pass variable values to sqlplus sql file from shell script

I am using the Properties file to get the DB connection values and below is shell script file.

....
read -p "Please enter start date and end date " para1 para2
source database.prop
for ((i=1;i<=$Dbcount;i++ )); do
sqlplus -S ${user1}/${Password}@${conn} <<EOF &
spool sqlcsvdb_"$i".csv
@squery.sql $para1 $para2
exit;
EOF
done
....

squery.sql file.

....
SET PAGESIZE 50000
SET COLSEP "|"
SET LINESIZE 20000
SET headsep ON
SET FEEDBACK OFF
SET TRIMSPOOL ON
SET TRIMOUT ON
set verify off 
SELECT id|| '|'||date|| '|'|| appid from table where date between '&para1' and '&para2';
exit;
EOF
....

When I execute shell it is not passing the variable values and getting ERROR at line 1:ORA-01722: invalid number error message. Please help me on how to I resolve this and usage of bind variables.

....
read -p "Please enter start date and end date " para1 para2
Date1=$para1
Date2=$para2
source database.prop
for ((i=1;i<=$Dbcount;i++ )); do
sqlplus -S ${user1}/${Password}@${conn} <<EOF &
spool sqlcsvdb_"$i".csv
@squery.sql $Date1 $Date2
exit;
EOF
done
....
....
SET PAGESIZE 50000
SET COLSEP "|"
SET LINESIZE 20000
SET headsep ON
SET FEEDBACK OFF
SET TRIMSPOOL ON
SET TRIMOUT ON
set verify off 
SELECT id|| '|'||date|| '|'|| appid from table where date between '&1' and '&';
exit;
EOF
....

Defining variables again in the shell script file then passing those variables to sql file works, until it is varaibles are defined within the shell script, it does not work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM