shell script error while running .sql file - oracle

While calling .sql file from shell script required data is retrieved but after completion - below errors are shown.
WHENEVER: not found.
SET: not found.
SET: not found.
SET: not found.
run command
sqlplus -s user/pasd#oracle_sid #file.sql
file.sql
WHENEVER SQLERROR EXIT 1
SET TERMOUT OFF
SET ECHO OFF
SET EMBEDDED ON
SET FEEDBACK OFF
SET HEADING OFF
spool file.log
select 'xyz' from dual;
spool off;
exit;

Related

not able to suppress sqlplus output on terminal

I am using following script to delimited file using spool in the unix server. I ssh into it using putty and execute the shell script.
Here is shell script:
/oracle/app/oracle/product/19.0.0/dbhome_1/bin/sqlplus -s /nolog<<-EOF
conn user/pass
SET PAGESIZE 0
SET LINESIZE 32000
SET NUMWIDTH 127
SET FEEDBACK OFF
set echo off
set heading off
set headsep off
SET MARKUP CSV ON DELIMITER | quote off
set termout off
spool mc_format.dat
select * from mytable;
spool off
EOF
but I am not able to suppress the rows printing out on the terminal, and eventually on log when I run using cron.
Is there any setting I am missing out?
Right from the documentation
SET TERMOUT OFF
Controls the display of output generated by commands in a script that is executed with #, ## or START. OFF suppresses the display so that you can spool output to a file without displaying the output on screen. ON displays the output on screen. TERMOUT OFF does not affect output from commands you enter interactively or redirect to SQL*Plus from the operating system.
So instead of the here document, call the script using one of the start methods mentioned.

Oracle SQL Script - Why spool creates file twice

I run the following SQL-script using Oracle SQL Developer. I don't know why it creates two spool file and it also displays the entire operations in the Oracle SQL Developer console which it should not. Maybe it is due to the SET FEEDBACK ON and SET FEEDBACK OFF for each delete statement.
It creates two files and displays everything in script-output only when there are some records for deletion.
The script's call
#"C:\RM.sql"
The script
SET PAGES 0
SET LINESIZE 10000
SET TRIMS ON
SET ECHO OFF
SET HEADING ON
SET VERIFY OFF
SET FEEDBACK OFF
SET TERMOUT OFF
SET SERVEROUTPUT on size 1000000
SET TIMING OFF
SET COLSEP '|'
alter session set NLS_DATE_FORMAT = 'dd.mm.yyyy hh24:mi:ss';
whenever oserror exit -1
whenever sqlerror exit -2
-- The current timestamps into 'times' variable
column times new_value times noprint
select to_char(sysdate, 'YYYYMMDD_HH24MISS') times from DUAL;
-- variables definition
define output_file="C:\temp\filename_&times..log"
-- echo some text to the standard output
SET TERMOUT ON
PROMPT
PROMPT "The script counts and reports the records to be deleted..."
PROMPT
SET TERMOUT OFF
-- write the results to the 'output_file' file
spool &output_file
PROMPT
PROMPT "BEGIN------------------------------"
PROMPT "delete from the_table1"
select 'Total records BEFORE delete: ' || count (*) as count_records from the_table1;
SET FEEDBACK ON
PROMPT "Actual deletion of records..."
delete from the_table1;
commit;
SET FEEDBACK OFF
PROMPT "END------------------------------"
PROMPT
PROMPT "BEGIN------------------------------"
PROMPT "delete from the_table2"
select 'Total records BEFORE delete: ' || count (*) as count_records from the_table2;
SET FEEDBACK ON
PROMPT "Actual deletion of records..."
delete from the_table2;
commit;
SET FEEDBACK OFF
PROMPT "END------------------------------"
PROMPT
PROMPT "The script completed..."
SPOOL OFF
SET TERMOUT ON
PROMPT
PROMPT "&output_file has been successfully ended."
PROMPT
PROMPT
EXIT
;
I seem to recall TERMOUT and ECHO options depend on how sqlplus is being run and can give different results, similar to what you are seeing.
I'm using sqlplus as a example of how interactions with the run environment can affect output. This isn't really an answer, and I cannot reproduce the scenarios I used to see often, but I hope the following perhaps will offer some pointers as to what might be encountered.
Start with a simple test script test_off.sql:
set echo off
set termout off
prompt text1
spool f1.txt
prompt text2
spool off
prompt text 3
quit
Now let's run it a number of different ways:
1 Non-interactively Parameterised
$ sqlplus -S un/pw#idb #test_off.sql
$ cat f1.txt
text2
No output to screen, spool file created, with only the spooled contents.
2. Interactively
$ sqlplus -S un/pw/#db
#test_off.sql
$ cat f1.txt
text2
No output to screen, spool file created, with only the spooled contents.
3. Run as piped feed
$ cat test_off.sql | sqlplus -S un/pw#db
text1
text2
text 3
Here we see all the PROMPT command results on screen.
Similar effects may be seen if run within a HEREDOC block.
I've definitely seen (in older versions) some strange inconsistencies and perhaps your SQL Developer is similarly afflicted.

What could be the possible error if a shell script fails when it is added to crontab?

I have a shell script that creates a text file with the data resulted by a sql query. It runs perfectly when I run it manually, but when the job is added to crontab, it fails as it is not able fetch few required parameters from a table in database. What are the things that change when we schedule the job using crontab?
[edit]
The script uses following function to execute a sql query that is passed as argument. when I run it manually, sql query that is passed is returning the desired value so script runs successfully. But when it is run via crontab, there is no data returned by the sql query hence script abnormally exits stating that required parameters are missing.
try_sql_spool() {
(
sqlplus -SILENT /NOLOG #/dev/stdin << EndOfSQL
WHENEVER OSERROR EXIT FAILURE
WHENEVER SQLERROR EXIT FAILURE
CONNECT $USER/$PWD#$SID
SET SERVEROUTPUT OFF;
SET TERMOUT OFF
SET TRIMSPOOL ON
SET PAGESIZE 0
SET LINESIZE 32767
SET FEEDBACK OFF
SET VERIFY OFF
SET TAB OFF
SPOOL /dev/stderr
$1
SPOOL OFF
EndOfSQL
) #3<&1 1<&2 2<&3 3<&-
}
Thanks in advance.
Answering own question:
Script was aborting when added to crontab because, user was switched and it could not spool /dev/stderr as new user didn't have write permission to /dev/stderr . so there was O/S Message: Permission denied error. so It could not fetch required data from data base.
Thanks all for your response.

importing data into CSV from a remote oracle server, I need to use a spool like command in Windows

I need to write a similar script in windows in a bat/cmd file. This script gets my data in csv on linux. I was wondering if there is any equivalent in windows box.
FILE="test.csv"
sqlplus username/passwd#database <<EOF
SET PAGESIZE 50000
SET COLSEP ","
SET LINESIZE 200
SET FEEDBACK OFF
SPOOL $FILE
SELECT * FROM test_table ;
SPOOL OFF
EXIT
EOF
Just use the redirection operator!
FILE="test.csv"
sqlplus username/passwd#database <<EOF >> %FILE%
SET PAGESIZE 50000
SET COLSEP ","
SET LINESIZE 200
SET FEEDBACK OFF
SELECT * FROM test_table ;
EXIT
EOF
You can use same commands in windows also, only thing you need is Oracle Client installed on the windows client machine, if it is a server then its ok. Create a .bat file and copy following script into it. Run the file from command prompt or double click, and csv file is created if database connection is successful and the table exits.
#echo off
sqlplus username/passwd#database
SET PAGESIZE 50000
SET COLSEP ","
SET LINESIZE 200
SET FEEDBACK OFF
SET TRIMSPOOL OFF
SPOOL test.csv
SELECT * FROM test_table ;
SPOOL OFF
EXIT

Oracle SqlPlus - saving output in a file but don't show on screen

Using SqlPlus for Oracle, how do I save the output of a query in a file but not show it on the terminal/prompt.
Right from the SQL*Plus manual
http://download.oracle.com/docs/cd/B19306_01/server.102/b14357/ch8.htm#sthref1597
SET TERMOUT
SET TERMOUT OFF suppresses the display so that you can spool output from a script without
seeing it on the screen.
If both spooling to file and writing to terminal are not required, use SET TERMOUT OFF in >SQL scripts to disable terminal output.
SET TERMOUT is not supported in iSQL*Plus
Try this:
SET TERMOUT OFF;
spool M:\Documents\test;
select * from employees;
/
spool off;
Try This:
sqlplus -s ${ORA_CONN_STR} <<EOF >/dev/null
set termout off doesn't work from the command line, so create a file e.g. termout_off.sql containing the line:
set termout off
and call this from the SQL prompt:
SQL> #termout_off

Resources