Execute the Start command with a variable in SQL*Plus - oracle

Is there a way to execute the start command with a variable as name. Actually, I need to provide a path that changes, but the filename is still the same, thus I decided to put a value in a user variable via Define or Accept
However, I receive error when I issue:
define name /home/andres/myfile.sql
start &name
-- Same error
start &name.
-- With an env variable, the same problem appears. (defining name in the shell)
start $name
start %name%
How can I execute a script with a dynamic name directly from SQLPlus? I know that I can do that from shell, but that will be platform dependent.
Finally, I am using CLPPlus, and that should have the same behavior as SQLPlus.

It seems to work for me on Unix. Next time please provide the error.
Your code errors : SP2-0136: DEFINE requires an equal sign (=)
Define needs an = sign.
dual.sql
select * from dual;
start.sql
start &1
define filename=/home/oracle/dual.sql
start &filename
Run script
SQL> #start dual
D
-
X
D
-
X

Related

Shell script to create loop for command

So i have a tcl code that i need to run in ns2. As we all know i just have to type 'ns abc.tcl' in terminal. In my abc.tcl code i have a variable x which i need to change and run the code. Is there any way i can write a script that will change the value of x and run 'ns abc.tcl' in terminal, then change the value again and run 'ns abc.tcl' in terminal again for a set of values for x. I believe i need to write a shell script but i don't know anything about that. Can you tell me the format i should write the script in like what should i write first and where do i write my values of x and how to make it run 'ns abc.tcl in terminal: 'function()' 'do' 'done' etc... If you can direct me to specific links about that would be helpful.
The easiest way, providing it works, is to pass the value in as an argument.
Invoke your code as ns abc.tcl TheValueToPassIn.
Access the value within your code by indexing into the argv global variable with lindex, which should contain a list of all arguments after the script name:
set myValue [lindex $::argv 0]
However, it's possible that that won't work (depending on exactly what the ns program does). If so, pass the value in inside an environment variable:
Invoke your code as MYVAR=TheValueToPassIn ns abc.tcl.
Access the value within your code by looking in the global env array:
set myValue $::env(MYVAR)
There are many other ways to do it, but those two are very easy.

Create batch file to run sql

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.

Backing up SQL Plus environment during script

Some scripts I have inherited will blindly call SET FEEDBACK OFF or SET ECHO OFF at the beginning of the script, then set them to ON or OFF at the end of the script. I would like to modify these scripts to determine what value was set before the script was run, and set the environment back to that value when the script completes.
How do I query SQL Plus environment values, store them, and restore them when a script has finished?
One method I have thought of:
SPOOL env-backup.sql
SHOW ECHO FEEDBACK TIMING
REM ...
#env-backup.sql
But
The values SHOW ECHO FEEDBACK TIMING spits out can't be executed directly (ECHO OFF vs SET ECHO OFF)
I would rather not create yet another file (or any modifications to the DB)
Not that it is necessarily related, but I'm using SqlPlus from Oracle XE (10g) on Windows
SQL*Plus has the STORE command just for this. It outputs a file which has all the environment settings. Executing the file would restore these settings. Type `HELP STORE' from the SQL*Plus prompt for more info.
You might be interested in those scripts
If you want each SQL to be run independently you could call them using the HOST command. That is, for misbehaving scripts call HOST SQLPLUS username/password#tnsname #script.sql and it will run in a new process.
Why not just put the desired values for your connection in the glogin.sql script provided?
It's normally found in %ORACLE_HOME%\sqlplus\admin
From my glogin.sql:
--
-- Copyright (c) 1988, 2005, Oracle. All Rights Reserved.
--
-- NAME
-- glogin.sql
--
-- DESCRIPTION
-- SQL*Plus global login "site profile" file
--
-- Add any SQL*Plus commands here that are to be executed when a
-- user starts SQL*Plus, or uses the SQL*Plus CONNECT command.
--
-- USAGE
-- This script is automatically run
--
set pagesize 60
set linesize 500
set wrap off
Then just reconnnect after running any scripts that alter your environment (or run #glogin.sql itself).

DOS/sqlplus "Handle is invalid" error?

I have the following batch script:
sqlplus ms/ms#orcl < drop.sql
sqlplus ms/ms#orcl < create.1.0.sql
This works fine when I double click on the bat file in Windows Explorer and run it.
But when I type the command name from the DOS prompt I get an error:
C:\>create.bat
C:\>sqlplus ms/ms#orcl 0<drop.sql
The handle is invalid.
C:\>sqlplus ms/ms#orcl 0<create.1.0.sql
The handle is invalid
Any ideas?
Update: Made the change to use # instead of <. This gets around the error but now the script only executes the first file and then leaves you at the SQL> prompt. To get the second file to execute you have to type exit at the prompt, then the second file runs. Not sure how to get both files to execute. ??
If you want SQL*PLUS to execute a script, a better way than command-line redirection is the # syntax:
sqlplus ms/ms#orcl #drop.sql
sqlplus ms/ms#orcl #create.1.0.sql
Also read up on the ## syntax, which you can use to execute a .sql script from another .sql script in the same directory.
SQL> help #
SQL> help ##
Made the change to use # instead of <.
This gets around the error but now the
script only executes the first file
and then leaves you at the SQL>
prompt. To get the second file to
execute you have to type exit at the
prompt, then the second file runs. Not
sure how to get both files to execute.
??
I ended up writing a python script to run sql plus and then exit it to get around this issue. I have yet to find any way around that limitation. Although you would only have to deal with the issue once if you use ## as was suggested. You would just have to run the second script from the first script.

oracle spool query

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

Resources