I want to know if its posible to execute in background a sqlplus like this
sqlplus -s "/ as sysdba" <<IN
select 1 from dual;
IN
I know that I can call a .sql file with nohup sqlplus ... #file.sql & but I want use the <
Something like this
nohup (sqlplus -s "/ as sysdba" <<IN
select 1 from dual;
IN) &
Or similar...
Regards,
You could do:
(
nohup sqlplus -s "/ as sysdba" <<IN
select 1 from dual;
IN
) &
You could do:
while [ 1 ]
do
sqlplus -s <<EOF
user/password#SID
WHENEVER SQLERROR EXIT SQL.SQLCODE
set feedback off
set serveroutput on
select 1 from dual;
EOF
sql_status=$?
if [ $sql_status -eq 0 ]
then
.
.
.
done
Related
I am trying to generate a csv by calling a .sql file. The issue is on execution an empty file is being created. Running the sql against the DB returns records. Apologies if this has already been answered but I was not able to find any answer that fixed my issue.
FILE="CASE.csv"
export dest_loc=/dest/loc
export LOGFILE=$LOG/100.$NOW_TS.log
echo "$0 started at $(date +"%Y-%m-%d:%H:%M:%S")" > ${LOGFILE}
sqlplus -s $DB_LOGIN<<EOSQL>>${LOGFILE}
SET echo OFF
SET feedback OFF
SET sqlprompt ''
SET TERMOUT OFF
SET UNDERLINE OFF
SET trimspool ON
SET trimout ON
SET SQLBLANKLINES ON
SET PAGESIZE 50000
SET COLSEP "|"
SET LINESIZE 650
SPOOL $dest_loc/$FILE
#$SRC/sql_to_run.sql
SPOOL OFF
EOSQL
echo "\nChecking for ORACLE errors..." >> ${LOGFILE}
grep 'ORA-' $LOGFILE >> ${LOGFILE}
rc=$?
if [ ${rc} = 0 ];
then
echo "\nThere has been an ORACLE error. RC = $RC" >> ${LOGFILE}
echo "\n$0 ended at $(date +"%Y-%m-%d:%H:%M:%S")" >> ${LOGFILE}
echo "####################################" >> ${LOGFILE}
exit 2
else
echo "\nNo ORACLE errors found." >> ${LOGFILE}
fi
The log file captured
./100.sh started at 2021-10-20:20:14:22 Checking for ORACLE errors...
No ORACLE errors found.
The reason why the csv file is empty is probably because you have an error that you are not capturing. sqlplus is a binary program, so it will exit always with 0, unless you use whenever sqlerror and whenever oserror in order to capture exceptions produced in the execution, either because of a database error or a operating system issue.
Prior to 12c
FILE="CASE.csv"
export dest_loc=/dest/loc
export LOGFILE=$LOG/100.$NOW_TS.log
echo "$0 started at $(date +"%Y-%m-%d:%H:%M:%S")" > ${LOGFILE}
sqlplus -s $DB_LOGIN << EOSQL >> ${LOGFILE}
WHENEVER SQLERROR EXIT FAILURE;
WHENEVER OSERROR EXIT FAILURE;
SET echo OFF
SET feedback OFF
SET sqlprompt ''
SET TERMOUT OFF
SET UNDERLINE OFF
SET trimspool ON
SET trimout ON
SET SQLBLANKLINES ON
SET PAGESIZE 50000
SET COLSEP "|"
SET LINESIZE 650
SPOOL $dest_loc/$FILE
#$SRC/sql_to_run.sql
SPOOL OFF
EOSQL
rc=$?
if [ ${rc} -ne 0 ];
then
echo "\nThere has been an ORACLE error. RC = $RC" >> ${LOGFILE}
echo "\n$0 ended at $(date +"%Y-%m-%d:%H:%M:%S")" >> ${LOGFILE}
echo "####################################" >> ${LOGFILE}
exit 2
else
echo "\nNo ORACLE errors found." >> ${LOGFILE}
fi
12c or higher
If you are in this version, you might want to use the feature set markup csv to generate files.
FILE="CASE.csv"
export dest_loc=/dest/loc
export LOGFILE=$LOG/100.$NOW_TS.log
echo "$0 started at $(date +"%Y-%m-%d:%H:%M:%S")" > ${LOGFILE}
sqlplus -s $DB_LOGIN << EOSQL >> ${LOGFILE}
WHENEVER SQLERROR EXIT FAILURE;
WHENEVER OSERROR EXIT FAILURE;
SET echo OFF
SET feedback OFF
SET SQLBLANKLINES ON
SET PAGESIZE 50000
SET LINESIZE 650
SET TERMOUT OFF
SET MARKUP CSV ON DELIMITER "|"
SPOOL $dest_loc/$FILE
#$SRC/sql_to_run.sql
SPOOL OFF
EOSQL
rc=$?
if [ ${rc} -ne 0 ];
then
echo "\nThere has been an ORACLE error. RC = $RC" >> ${LOGFILE}
echo "\n$0 ended at $(date +"%Y-%m-%d:%H:%M:%S")" >> ${LOGFILE}
echo "##########################################" >> ${LOGFILE}
exit 2;
else
echo "\nNo ORACLE errors found." >> ${LOGFILE}
fi
Test Sqlplus Exit Status
$ sqlplus / as sysdba
SQL*Plus: Release 19.0.0.0.0 - Production on Thu Oct 21 08:32:27 2021
Version 19.6.0.0.0
Copyright (c) 1982, 2019, Oracle. All rights reserved.
Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.6.0.0.0
SQL> create table t1 ( c1 number );
create table t1 ( c1 number )
*
ERROR at line 1:
ORA-00955: name is already used by an existing object
SQL> exit
Disconnected from Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.6.0.0.0
[ftpcpl#scglvdoracd0006 ~]$ echo $?
0
$ sqlplus / as sysdba
SQL*Plus: Release 19.0.0.0.0 - Production on Thu Oct 21 08:32:43 2021
Version 19.6.0.0.0
Copyright (c) 1982, 2019, Oracle. All rights reserved.
Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.6.0.0.0
SQL> whenever sqlerror exit failure;
SQL> create table t1 ( c1 number );
create table t1 ( c1 number )
*
ERROR at line 1:
ORA-00955: name is already used by an existing object
Disconnected from Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.6.0.0.0
$ echo $?
1
Here is the shell script which need to trigger a stored procedure and when the record count is 0 then it should capture in the log that 0 duplicate records are found and if it's more than 0 then log with no of records.
Also if there is any sqlerror then it should write the same in the log file and it should send the communication in all scenarios.
We have funct file which has declared with all directories and we are referring here for the log file path directory.
Could you please help me to correct the code logic?
#!/bin/ksh
. /local/dir1/funct.sh
sqlplus -s ${ORAUSER}/${ORAPASSWD}#${ORASRVC} <<EOF > $LOG_TEXT
set head off
set serveroutput on
SELECT TO_CHAR(SYSDATE,'MMDDRRRRHH24MISS') FROM DUAL;
EOF
TIME_STAMP=`cat ${LOG_TEXT}`
LOG_FILE_NAME='FileStatus'${TIME_STAMP}'.log'
LOG_FILE=${LOGFILEDIR}'/FileStatus/'${LOG_FILE_NAME}
write_log "Database Timestamp is $TIME_STAMP"
write_log "Executing FileStatus now..."
sqlplus -s ${ORAUSER}/${ORAPASSWD}#${ORASRVC} <<EOF >> ${LOG_TEXT}
set feedback off
set heading off
DECLARE
v_count NUMBER;
BEGIN
select count(*) INTO v_count from (select col1,col2,count(col3) from Tab1 group by col1,col2 having count(col3)>1);
whenever sqlerror exit -1
whenever oserror exit -1
execute FileStatus;
END;
EOF
if [[ v_count -eq 0 ]] then
write_log "FileStatus executed successfully."
write_log "No Duplicate records Found: $LOG_FILE"
mailx -s "No Duplicate records Found" XXXXX#gmail.com < ${success}
elif [[ "$v_count -gt 0 ]]
then
write_log "FileStatus executed successfully."
write_log "Number of duplicate records found::$v_count:$LOG_FILE"
mailx -s "Number of duplicate records found:$v_count" XXXXX#gmail.com < ${success}
else
Write_log "FileStatus Failure."
mailx -s "FileStatus" XXXXX#gmail.com < ${failure}
fi
cleanup
exit 0;
I have a stored procedure in SQL written and I am trying to write the info from that procedure to an output file using shell script. I use spool, but I do not see the output file after the script was run.
My script:
run_procedure(){
userId="abc"
psswd="abcd"
package="Retrieve_data"
procedure="RETRIEVE_SERVER_DATA"
echo ${package}.${procedure}
sqlplus ${userId}/${passwd} >dev/null 2>&1 <<EOF
set serveroutput on;
set line 7000;
set feedback off;
set termout off;
spool /tmp/test.txt;
EXECUTE ${package}.${procedure};
spool off;
exit;
EOF
)
Here is where I call the above function:
if [[ $RETURN -eq 0 ]]; then
run_procedure
fi
(My return is equal to 0, by the way.)
I have script below i am using get value of one parameter
var=`ssh -X -o StrictHostKeyChecking=no $phost "
cd ; . ./.bash_profile;cd $ORACLE_HOME/dbs; sqlplus -s / as sysdba <<EOF
set heading off
set pagesize 0
set tab off
set feedback off
select value from v$parameter where name='control_files';
EOF
"`
this script this fetching value from table v$parameter and assigning it to variable var, now how can i escape $ from v$parameter
i have tried v\$parameter but it didnt help
You can cat the code lines into a file during the script run and then run the created script.
Something like:
echo "set heading off" > tempfile.sh
echo "set pagesize 0" >> tempfile.sh
echo "set tab off" >> tempfile.sh
echo "set feedback off" >> tempfile.sh
echo "select value from v${parameter} where name='control_files';" >> tempfile.sh
chmod +x tempfile.sh
./tempfile.sh
I have shell script that calls the following sql script:
INSERT INTO SEMANTIC.COUNT_STATISTICS (...);
UPDATE SEMANTIC.COUNT_STATISTICS
SET PRNCT_CHANGE = 1.1;
--want to store result of this bellow select statement in model_count variable
select PRNCT_CHANGE
FROM SEMANTIC.COUNT_STATISTICS
WHERE model = '&MY_MODEL'
AND NEW_DATE = (
select max(NEW_DATE)
from SEMANTIC.COUNT_STATISTICS
where MODEL = '&MY_MODEL'
);
Now, how do I return this PERCENTAGE_NUMBER variable back to my shell script?
My shell script is as follows:
#!/bin/bash
#
# setup oracle, java, and d2rq environment
. /etc/profile.d/oracle.sh
. /etc/profile.d/java.sh
. /etc/profile.d/d2rq.sh
cd /opt/D2RQ
model_count=$(sqlplus user/pass #count.sql 'MODEL')
if ["$model_count" > 0]; then
echo "percentage count is positive"
else
echo "its negative"
I would like for that last SELECT statement result to be stored into my model_count variable in shell script.
Anyone knows why is not working?
A bash example with the use of a bash-function (note! database OS-authentication "/")
#!/bin/bash
get_count () {
sqlplus -s / <<!
set heading off
set feedback off
set pages 0
select count(*) from all_objects where object_type = '$1';
!
}
count=$(get_count $1)
echo $count
if [ "$count" -gt 0 ]; then
echo "is greater than zero"
else
echo "is less or equal to zero"
fi
~/tmp/ $ ./count.sh INDEX
2922
is greater than zero
~/tmp/ $ ./count.sh TABLE
1911
is greater than zero
~/tmp/ $ ./count.sh FUNCTION
226
is greater than zero
~/tmp/ $ ./count.sh "SUPEROBJECT"
0
is less or equal to zero
What I actually did is I separated those 2 queires and called them separatelly in my shell script:
sqlplus -S user/pass << EOF
whenever sqlerror exit 1;
set echo on
#/opt/D2RQ/model_count.sql '$MODEL' <--model_count.sql still has those INSERT & UPDATE statements
exit;
EOF
model_count=`sqlplus -S user/pass << EOF
SELECT PRNCT_CHANGE
FROM COUNT_STATISTICS
WHERE model = '$MODEL'
AND NEW_DATE = (
select max(NEW_DATE)
from COUNT_STATISTICS
where MODEL = '$MODEL'
);
exit;
EOF`
if [ $model_count >= 0 ]; then
echo "$model_count"
else
echo "'$MODEL' is negative " | mail -s "scripts issues" -c angelina1984#aol.com
fi