How to use sqlplus and sqlldr in a script file? - oracle

I am using sqlplus to get data from a remote database table and then using sqlldr to insert that data into some other table. I have written a shell script to do this. The sqlplus option is working but the sqlldr command is not working, although I have been able to insert using it from command prompt but it seems not be working in file.
Here's my script.
#!/bin/bash
#script to get 5 minutes of data from FCT table and insert into LOCAL table
sqlplus -s user/passs#FCT_DB <<EOF
SET TERMOUT OFF
SET LINESIZE 300
SET HEAD OFF
SPOOL export.csv
SELECT X FROM Y
SPOOL OFF
EXIT
EOF
! SQLLDR usr/pass#LOCAL_DB CONTROL=loader.ctl ERRORS=5000
The export.csv is created correctly. loader.ctl is also correct.
Also, whenever the script runs it shows data fetched on shell. I have searched that SET TERMOUT OFF helps not to display it, but it also doesn't seem to work.

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.

Run sqlplus commands from multiple files with query logging or spool in Windows Batch File

I am quite new to batch scripting, and I am trying to run multiple sql files, which in turn may contain multiple sql DML/DDL queries from bat file. The output files must contain all the queries being executed and the query output. Unlike this example , I don't have spool command inside my sql file, and I can not edit the input sql files. The following command works for me in ksh file (thanks to here-document):
$sqlplus /nolog <<! >>$sqlLogs.lst
connect $USERNAME/$PASSWORD#${DBNAME}
set echo on timing on
spool ${SCRIPTRUNFILE_SPOOL}
select name from v\$database;
#${SCRIPTRUNFILE};
spool off
exit
!
I want the exact same in Windows bat file. I have tried using ^. I can't do combine all sql files into one, as I need logging for each sql file into different file. My attempt at the bat file script is as follows and I have played around this much, and it fails with spool command not recognized. I also prefixed below commands with sqlplus, but still unable to achieve something like above ksh file:
sqlplus -s username/pwd#DBName >> sqlLogs.lst
set echo on timing on
spool %RUNFILENAME%.lst
#%RUNFILENAME% > %RUNFILENAME%.lst
select name from v\$database;
spool off
quit
Following logic executes my scripts but does not log the query being executed. Also, I don't want to connect twice to the database.
echo select name from v$database; | sqlplus -s username/pwd#DBName >> sqlLogs.lst
echo quit | sqlplus -s username/pwd#DBName #%SCRIPTRUNFILE%>> %SCRIPTRUNFILE_SPOOL%.lst
Can someone here please help to spool to a file where I can log the queries as well, while maintaining a single DB Connection?
Pass your immediate SQL*Plus commands to your sqlplus via stdout, grouped together by Windows' ( and ) symbols...
(
echo.set echo on timing on
echo.spool %SCRIPTRUNFILE_SPOOL%
echo.select name from v$database;
echo.#%SCRIPTRUNFILE%
echo.spool off
echo.exit
) | sqlplus -s %USERNAME%/%PASSWORD%#%DBNAME% >> sqlLogs.lst

Oracle SQL Developer, I keep getting the query in the .CSV file [duplicate]

I am wanting to output a Query to a CSV file and am using the below as a small test;
spool c:\test.csv
select /*csv*/ username, user_id, created from all_users;
spool off;
but the output has the actual select statment as the first line
> select /*csv*/ username user_id created from all_users
USERNAME USER_ID CREATED
REPORT 52 11-Sep-13
WEBFOCUS 51 18-Sep-12
Is there a way to prevent this? I tried SET Heading Off thinking that might do it, but it did not change. I am using SQL Developer an running as script.
Thanks
Bruce
Unfortunately SQL Developer doesn't fully honour the set echo off command that would (appear to) solve this in SQL*Plus.
The only workaround I've found for this is to save what you're doing as a script, e.g. test.sql with:
set echo off
spool c:\test.csv
select /*csv*/ username, user_id, created from all_users;
spool off;
And then from SQL Developer, only have a call to that script:
#test.sql
And run that as a script (F5).
Saving as a script file shouldn't be much of a hardship anyway for anything other than an ad hoc query; and running that with # instead of opening the script and running it directly is only a bit of a pain.
A bit of searching found the same solution on the SQL Developer forum, and the development team suggest it's intentional behaviour to mimic what SQL*Plus does; you need to run a script with # there too in order to hide the query text.
My shell script calls the sql file and executes it. The spool output had the SQL query at the beginning followed by the query result.
This did not resolve my problem:
set echo off
This resolved my problem:
set verify off
Just for anyone who stumbles upon this (after 7+ years) ...
This works for me ... adapt it to your way of invoking sqlplus.
sqlplus -s / as sysdba 2>&1 > /dev/null <<EOF
whenever sqlerror exit sql.sqlcode;
set echo off
set verify off
set feedback off
set term off
set head off
set pages 1000
set lines 200
spool /tmp/whatever.output
### Your_SQL_statement_here ###
example: select sysdate from dual;
example: select * from V\$LOGFILE;
spool off
EOF
-s flag here is the key to not displaying the SQL statements when runing a script.
set echo off
spool c:\test.csv
select /*csv*/ username, user_id, created from all_users;
spool off;
You can directly export the query result with export option in the result grig.
This export has various options to export. I think this will work.
Exec the query in TOAD or SQL DEVELOPER
---select /*csv*/ username, user_id, created from all_users;
Save in .SQL format in "C" drive
--- x.sql
execute command
---- set serveroutput on
spool y.csv
#c:\x.sql
spool off;

Spool Command: Do not output SQL statement to file

I am wanting to output a Query to a CSV file and am using the below as a small test;
spool c:\test.csv
select /*csv*/ username, user_id, created from all_users;
spool off;
but the output has the actual select statment as the first line
> select /*csv*/ username user_id created from all_users
USERNAME USER_ID CREATED
REPORT 52 11-Sep-13
WEBFOCUS 51 18-Sep-12
Is there a way to prevent this? I tried SET Heading Off thinking that might do it, but it did not change. I am using SQL Developer an running as script.
Thanks
Bruce
Unfortunately SQL Developer doesn't fully honour the set echo off command that would (appear to) solve this in SQL*Plus.
The only workaround I've found for this is to save what you're doing as a script, e.g. test.sql with:
set echo off
spool c:\test.csv
select /*csv*/ username, user_id, created from all_users;
spool off;
And then from SQL Developer, only have a call to that script:
#test.sql
And run that as a script (F5).
Saving as a script file shouldn't be much of a hardship anyway for anything other than an ad hoc query; and running that with # instead of opening the script and running it directly is only a bit of a pain.
A bit of searching found the same solution on the SQL Developer forum, and the development team suggest it's intentional behaviour to mimic what SQL*Plus does; you need to run a script with # there too in order to hide the query text.
My shell script calls the sql file and executes it. The spool output had the SQL query at the beginning followed by the query result.
This did not resolve my problem:
set echo off
This resolved my problem:
set verify off
Just for anyone who stumbles upon this (after 7+ years) ...
This works for me ... adapt it to your way of invoking sqlplus.
sqlplus -s / as sysdba 2>&1 > /dev/null <<EOF
whenever sqlerror exit sql.sqlcode;
set echo off
set verify off
set feedback off
set term off
set head off
set pages 1000
set lines 200
spool /tmp/whatever.output
### Your_SQL_statement_here ###
example: select sysdate from dual;
example: select * from V\$LOGFILE;
spool off
EOF
-s flag here is the key to not displaying the SQL statements when runing a script.
set echo off
spool c:\test.csv
select /*csv*/ username, user_id, created from all_users;
spool off;
You can directly export the query result with export option in the result grig.
This export has various options to export. I think this will work.
Exec the query in TOAD or SQL DEVELOPER
---select /*csv*/ username, user_id, created from all_users;
Save in .SQL format in "C" drive
--- x.sql
execute command
---- set serveroutput on
spool y.csv
#c:\x.sql
spool off;

Resources