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

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.

Related

Get Oracle output in txt file in UNIX

I've below script, The issue I'm facing is that I'm not getting the sql statement output in the file.
I don't understand which part is wrong. Can someone please help.
#!/bin/bash
echo "-----------------------------------------------------"
echo "Start Date, `date`"
PATH= --*path*--
ORACLE_HOME= --*path*--
TNS_ADMIN= --*path*--
export PATH
export ORACLE_HOME
export TNS_ADMIN
FILE="/tmp/output_Oracle.txt"
sqlplus -S user/pass#service<< EOF
set pagesize 50000
set colsep ","
set linesize 200
set feedback off
set heading off;
set serveroutput off;
spool $FILE
--#SQL statement
spool off
exit;
EOF
exit 0

How to capture the output of a PL SQL function in a powershell variable?

I am able to connecting to remote oracle database using powershell and sqlplus. Also I am able to capture the output of the command/query executed in the database into a powershell variable.
PFB example.
$username ="hr"
$password ="hr"
$tnsalias ="orcl"
$outputfilepath="C:\Users\Desktop"
$sqlquery=#"
set serveroutput off
set feedback off
set heading off
set echo off
select sysdate from dual;
"#
Running the above query in oracle database as below
$xval=$sqlquery | sqlplus -silent $username/$password#$tnsalias
echo $xval
The output will be
23-APR-18
The problem is when I am running a PL SQL function as below.
$sqlquery=#"
set serveroutput off
set feedback off
set heading off
set echo off
declare
x varchar2:=null;
exec x:=MYTEST_FUN('a','b');
"#
There is no value captured in the $xval variable and also the function is not running.
How to execute the function such that the return value of the function is captured in a powershell variable.
One way is to make it set serveroutput on and use DBMS_OUTPUT.PUT_LINE() within BEGIN..END
Another option is to use the sqlplus' PRINT command.
set serveroutput off
set feedback off
set heading off
set echo off
variable x VARCHAR2 --declare bind variable x
exec :x := MYTEST_FUN('a','b'); --a colon before x needed.
PRINT x;
Worked for me:
$sqlQuery = #"
set serveroutput off
set feedback off
set heading off
set echo off
variable x NUMBER;
select count(*) into :x from TABLE;
PRINT x;
exit
"#
($sqlQuery | sqlplus -S user/pass | Write-Output | Out-String).Trim()
Late entry, most concisely:
$dateVar = ("set heading off
select sysdate from dual;" | sqlplus -s $yourConnectionString | Out-String).Trim()

Pass Table name as Variable in sql query in Unix Shell Script Solaris

I'm creating a Unix shell script in solaris on which my task is to run the sql queries for 35 tables and bring the output to a csv file.For Which what I thought is that I will create an array of and by using for loop I will the pass the table name one by one to sql query.So as a sample I'm trying to pass the table name as variable in sql query.
I'm passing table name is sql query as well as the CSV which I m creating.
But is not working.Please help....
table_nm="PRODUCT"
sqlplus -s admin/admin_123#extend12 <<EOF
SPOOL /data2/interfaces/scripts/`$table_nm`.CSV;
set colsep ,
set feedback off
set trimspool on
set linesize 5000
set pagesize 1000
set heading on
set term off
set verify off
set timing off
set echo off
select * from `$table_nm` where PROD_ID = '1618' AND PROD_SER_NUM = 21 ;
spool off;
EXIT;
EOF
echo "end"
Loop through the array. Don't use back ticks.
tablearry=(PRODUCT SALES EMPLOYEES)
for i in "${tablearry[#]}"
do
table_nm=$i
sqlplus -s admin/admin_123#extend12 <<EOF
SPOOL /data2/interfaces/scripts/${table_nm}.CSV;
set colsep ,
set feedback off
set trimspool on
set linesize 5000
set pagesize 1000
set heading on
set term off
set verify off
set timing off
set echo off
select * from ${table_nm} where PROD_ID = '1618' AND PROD_SER_NUM = 21 ;
spool off;
EXIT;
EOF
echo "end"
done

call a procedure with date parameter in sqlplus oracle using shell script

I want to call a procedure in sqlplus oracle using shell script,
my procedure name is getdate_proc with two parameters, startdate and enddate.
I want to set startdate = sysdate and enddate = sysdate + 5 days
for example :execute getdate_proc(to_date('05/05/2015', 'MM-DD-YYYY'),to_date('05/09/2015','MM-DD-YYYY'))
below is my code:
#!/usr/bin/ksh
sqlplus -s /nolog << EOF
connect scott/tiger
--execute procedure with parameter
execute getdate_proc(to_date(sysdate, 'MM-DD-YYYY'),to_date(sysdate + 5days,'MM-DD-YYYY'))
--set spooling to save in csv
set underline off
SET RECSEP OFF
set verify off
set colsep ','
set linesize 300
set trimspool on
spool /home/user/project/samp.csv
select * from att2;
spool off
set verify off
i solved my problem with this code:
#!/usr/bin/ksh
date1='date'
date1=$(/bin/date --date="$date1" -d "+0 day" +"%F")
date2=$(/bin/date --date="$date1" -d "+4 day" +"%F")
echo $date1
echo $date2
sqlplus -s /nolog << EOF
connect scott/tiger
--execute procedure with parameter
execute attendance(to_date('$date1', 'YYYY-MM-DD'),to_date('$date2','YYYY-MM-DD'))
--set spooling to save in csv
set underline off
SET RECSEP OFF
set verify off
set colsep ','
set linesize 300
set trimspool on
spool /home/user/project/samp.csv
select * from att2;
spool off
set verify off
:) cheers

How to connect Data Base

I want to connect DB Using Unix shell script but i an not able to connect
below mention Code
DB
export ORACLE_HOME="oracle"
value=`/oracle/bin/sqlplus -S user/pwd#SHM4EQ << EOF
SET LINESIZE 300
SET PAGESIZE 300
SET HEADING OFF
SELECT * form dual
EOF`
Try like this...
#!/bin/bash
LogDirectory='/var/tmp/logs'
DataDirectory='/var/tmp/data'
DBUSER='scott'
DBUSERPASSWORD='tiger'
DBSID='oracle'
sqlplus -s ${DBUSER}/${DBUSERPASSWORD}#${DBSID} <<EOF
set linesize 32767
set feedback off
set heading off
select * from dual;
EOF

Resources