I have written the sql by name cashload.txt on unix box and kept it on the following location on unix box:
exit |sqlplus -s batch/password#SW_TEST #/soft/checkfree/AccurateBXG/scripts/cashload.txt
In the cashload.txt the below code is written:
spool /Detail/reports/inner/SW/Rep_OIbyAccount_$DATE_FILE.csv
select accountnumber||','||accountname||','||X from HSBC_Cash_OIbyAccount_v;
spool off
But it is not spooling the result set on the above mentioned path.However,when I am giving the path where the script is kept,It is spooling at that location.I don't understand why?It is spooling at the below path where the cashload.txt(sql script) is kept:
**spool /soft/checkfree/AccurateNXG/scripts/Rep_OIbyAccount.csv**
select accountnumber||','||accountname||','||X from HSBC_Cash_OIbyAccount_v;
spool off
Please look into the above query and help me out.
Thanks in advance!!!
If I understand this correctly, DATE_FILE is a shell variable that you want to substitute into the spool file name. This might be part or all of your problem. I don't think you can reference this directly from SQLPlus. You would need to pass that in as a parameter to the script then reference it as a SQLPlus substitution variable.
So the command line would be:
sqlplus -s batch/password#SW_TEST #/soft/checkfree/AccurateBXG/scripts/cashload.txt $DATE_FILE
And the spool command in the script would be:
spool /Detail/reports/inner/SW/Rep_OIbyAccount_&1..csv
(The extra period is necessary because it serves as a terminator to the substition variable name.)
Almost certainly it is a file permissions problem. Remember that when we interact with the OS from inside the database we are using the oracle account, not the account we connected as.
So does the oracle account have write permissions on /Detail/reports/inner ?
Can you echo the value of "/Detail/reports/inner/SW/Rep_OIbyAccount_$DATE_FILE.csv" ?
Only reason I can think of are
a) The above location is not a valid path
b) You do not have permissions to write to that location.
Do you get an error in the first case, or does nothing happen at all ?
Does running without the silent (-s) option give you any more detail?
The path mentioned by you has a $ symbol which is causing the problem. To avoid that just give the path and file name in " ". That should solve the problem:
SPOOL "/Detail/reports/inner/SW/Rep_OIbyAccount_$DATE_FILE.csv "
Maybe you can try the following to see if it generats the desired output. It will put in the directory which you are currently in.
set termout off
set echo off
set linesize 140
set feedback off
set pagesize 0
spool DATE_FILE.csv
select accountnumber
||','
||accountname
||','
|| X
from HSBC_Cash_OIbyAccount_v;
spool off
ed DATE_FILE.csv
Related
I'm trying to create a batch file to run a sql query (which then spools the result).
However, at the moment nothing happens, I just get usage info about sqlplus in the cmd window. Here is my batch code:
SQLPLUS login/password#(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=<url>)(Port=<port>))(CONNECT_DATA=(SID=<sid>)))#C:\Users\Documents\SQL\test2.sql
pause
and here is my test2.sql file:
spool C:\Users\Documents\Testing.csv
select *
from test;
spool off;
exit;
Please help - what am I doing wrong? (I'm very new to this so please answer/ask/ridicule in simple terms so I can understand)
It's something obvious you're doing wrong. You need a space between the end of the connect string and the # with the script file name:
SQLPLUS login/password#(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=<url>)(Port=<port>))(CONNECT_DATA=(SID=<sid>))) #C:\Users\Documents\SQL\test2.sql
The <url> reference is odd here; hopefully that is actually hostname, fully-qualified hostname (that can be resolved), or an IP addrss.
However, I'd suggest you use a TNS alias so you can shorten the command line, putting all that information in a tnsnames.ora file instead; or if you know the service name (which might be the same as the SID) use the easy connect syntax:
SQLPLUS -l -s login/password#//<hostname>:<port>/<service_name> #C:\Users\Documents\SQL\test2.sql
I've also thrown in the -s flag to suppress the banner, and the -l flag so it stops if the credentials or connect string are wrong. Your original ersion would have tried to use the script contents to log in after the initial failure, producing even more confusing output.
I'm trying to run the following SQLPlus* set of commands:
/usr/tmp/> sqlplus -s / #my_test_script param1 param2 <<-EOF
SET ECHO OFF
SET HEADING OFF
SET VERIFY OFF
SET TERMOUT OFF
SET FEEDBACK OFF
SET PAGES 0
SET LINESIZE 400
EXIT
EOF
The SET commands are supposed to suppress the output from the script I'm running - but they have no effect, since the script itself probably has a different "scope" for ECHO, HEADING, etc.
The output is suppressed only once I move all those SET commands into the script-file itself.
Since this piece of code should eventually turn into a generic script for running other SQL scripts, putting those SET command inside every script is not a good solution.
Does anyone know of a way to force the SET command values over scripts ran within the same SQLPlus* "session"?
I agree with Shannon. You could try the following code (which changes the ordering a little bit):
/usr/tmp/> sqlplus -s / <<-EOF
SET ECHO OFF
SET HEADING OFF
SET VERIFY OFF
SET TERMOUT OFF
SET FEEDBACK OFF
SET PAGES 0
SET LINESIZE 400
#my_test_script param1 param2
EXIT
EOF
if I remember well, you could call from every script this initializing script using ## command. for example:
##init.sql
source
EDIT
also, from the oracle documentation :
SQL*Plus also supports a User Profile, executed after the Site Profile. This file is generally named login.sql. SQL*Plus searches for the user profile in your current directory, and then the directories you specify with the SQLPATH environment variable. SQL*Plus searches this colon-separated list of directories in the order they are listed.
You can add any SQL commands, PL/SQL blocks, or SQL*Plus commands to your user profile. When you start SQL*Plus, it automatically searches for your user profile and runs the commands it contains.
I need to update a table with some where conditon. If update fails then I need to insert the same. I am using here document from a shell script to run sqlplus, please help me to find the way so I can insert in case of faliur of update.
Redirect the output to a temporary file and don't forget to include stderr as well. Then you can parse the output and look for characteristics. In your SQL you can also do a
select "SUCCESS" from dual;
at the end, the shell script could look for.
sqlplus user#db #my_sqlfile >output 2>&1
grep "SUCCESS" output
sqlplus also usually writes in the output something like
N records updated.
So you can also check if there are as many updates as you expect if you need this.
Just be sure to set the options in your sql file appropriately because this affects what sqlplus will spill out.
set term ...
Using sqlplus.exe I'm looking for a way to write sqlplus output to a file.
Is there anyway I can do that, currently the output is written only to the console.
You may use the SPOOL command to write the information to a file.
Before executing any command type the following:
SPOOL <output file path>
All commands output following will be written to the output file.
To stop command output writing type
SPOOL OFF
Also note that the SPOOL output is driven by a few SQLPlus settings:
SET LINESIZE nn - maximum line width; if the output is longer it will wrap to display the contents of each result row.
SET TRIMSPOOL OFF|ON - if set OFF (the default), every output line will be padded to LINESIZE. If set ON, every output line will be trimmed.
SET PAGESIZE nn - number of lines to output for each repetition of the header. If set to zero, no header is output; just the detail.
Those are the biggies, but there are some others to consider if you just want the output without all the SQLPlus chatter.
Make sure you have the access to the directory you are trying to spool. I tried to spool to root and it did not created the file (e.g c:\test.txt). You can check where you are spooling by issuing spool command.
just to save my own deductions from all this is (for saving DBMS_OUTPUT output on the client, using sqlplus):
no matter if i use Toad/with polling or sqlplus, for a long running script with occasional dbms_output.put_line commands, i will get the output in the end of the script execution
set serveroutput on; and dbms_output.enable(); should be present in the script
to save the output SPOOL command was not enough to get the DBMS_OUTPUT lines printed to a file - had to use the usual > windows CMD redirection. the passwords etc. can be given to the empty prompt, after invoking sqlplus. also the "/" directives and the "exit;" command should be put either inside the script, or given interactively as the password above (unless it is specified during the invocation of sqlplus)
In windows type this command:
sqlplus.exe username/password#servicename #yourquery.sql > out.txt
besides, you should write an exit command at the end of "yourquery.sql" file.
I have a problem with insertion scripts in SQL PLUS.In my insert scripts data contains '&' like this 'Rat&CAT'.When am run my script file using SQL PLUS Batch file .It is always promting this message "Enter the Value of CAT:".Could any one tell me how to solve this problem?
Add this command at the start of your script:
SET DEFINE OFF;
It will turn off variable substitution.