How to use sql*plus in Windows command script to control flow? - windows

I'm trying to use sql*plus to control a small Windows command script.
Basically, I want to execute some PL/SQL (perhaps select from a view or table or execute a function) which shows me the status of some rows in the database, and then depending upon the state of the rows, perform some Windows commands.
My problem is how to get the results back into the command script.
sqlplus user/password#server #script.sql
IF <CONDITIONAL HERE BASED on script.sql results> GOTO :runprocess
REM log and email that process had to be skipped
EXIT
:runprocess
REM run various Windows service commands

I'd probably write the script (or the conditional, depending on the requirements) from the called script.sql itself.
For example, the following script.sql creates a .bat file windows_commands.bat:
set feedback off
set echo off
set trimspool on
set termout off
set serveroutput on size 100000 format wrapped
set lines 500
set pages 0
-- create the bat file to be executed later:
spool windows_commands.bat
declare
c number;
begin
select count(*) into c from dual;
-- depending on a conditional, write the stuff to be executed into the
-- bat file (windows_commands.bat)
if c = 1 then
dbms_output.put_line('#echo everthing ok with dual');
else
dbms_output.put_line('#echo something terribly wrong with dual');
end if;
end;
/
spool off
exit
You can then call script.sql from yet another .bat file like so:
#rem create oracle session, call script.sql
sqlplus %user%/%password%#%db% #script.sql
#rem script.sql has created windows_commands.bat.
#rem call this newly created bat file:
call windows_commands.bat

This is what I ended up using.
My .cmd script:
#ECHO OFF
ECHO Checking Oracle...
for /f %%i in ('sqlplus -s user/password#database #script.sql') do #set count=%%i
echo %count%
IF %count% GTR 0 GOTO :skipped
GOTO :runprocess
Where script.sql:
SELECT COUNT(*)
FROM table
WHERE criteria = 1;
exit

I would strongly encourage you to not use .bat files. You've got lots of other alternatives: C/C++ or VB, Windows scripting or Powershell, or even free downloads like Perl or Bash.
But here's one example of returning error codes in .bat files:
http://www.dbforums.com/oracle/1044496-sqlplus-return-value-help.html
But please do look at some of the links I gave above. Avoiding .bat files will make it easier for you, and make it easier to maintain in the future.
IMHO ...

I do something like this by creating a .bat file which does the windows stuff and calling sql scripts as needed. Use SQL to spool your results to a text file which you can read.
...dos commands here
sqlplus /nolog #C:\Dump\DropRecreateUsers.sql
sqlplus /nolog #C:\Dump\Cleanup.sql
...dos commands
In the sql use this command spool C:\yourResults.txt or for more sophisticated usages create a procedure, which, when called, writes the results to a text file using UTL_FILE

I encourage you to take a look at the two scripts included in the Oracle XE for backup and restore. These scripts have taught me a lot how to handle batch-scripting and Oracle on the Windows platform.
C:\oraclexe\app\oracle\product\11.2.0\server\bin\Backup.bat
C:\oraclexe\app\oracle\product\11.2.0\server\bin\Restore.bat

Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> #f:\testa.txt

Related

Cant able to run sqlplus in windows batch script || ORA-28040: No matching authentication protocol exception

I am using below batch script and want to execute a SQL query and save the data in CSV file
call sqlplus myuser/mypass#abcdb#"(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=xyz-scan.abc.com)(PORT=1521))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=abcdb)))" #C:\myfolder\myquery.sql
but it is giving me "ORA-28040: No matching authentication protocol exception" error when i run my batch script
I also tried with simple TNS (note: my tns location is "D:\app\iis_admin\product\11.1.0\client_1\network\admin\sample")
call sqlplus myuser/mypass#abcdb #C:\myfolder\myquery.sql
But again getting same error.
my myquery.sql contain below query
set feed off
set pagesize 0
spool C:\myfolder\myresult.csv
prompt sysdate
select to_char(sysdate, 'dd-mon-yyyy') from dual;
spool off
set head on
set feed 6
quit
I presume it might be about too large gap between versions you use.
screenshot suggests it is Oracle 8.0.6 (gosh, that's old! Dates in 1997)
text mentions product\11.1.0
Therefore, I'd upgrade the former so that it would be able to speak with the latter.

Query for command prompt in Aspen SQLPlus

How do you access the command prompt using Aspen SQLPlus?
I have tried using Host Dir, but does not seem to work.
You need to use the SYSTEM command. This is covered quite well within the Aspen SQLPlus help menu.
E.g.
SYSTEM ('DIR');
SYSTEM ('DIR C:\WINDOWS\');
SYSTEM ('ROBOCOPY C:\SCRATCH\ C:\USERS\JOHN\DESKTOP\ TEST.TXT');
SYSTEM ('HOSTNAME');
SYSTEM ('WHOAMI');
Will print the results to screen.
Alternatively, you can format the results as a table:
select line, linenum from SYSTEM ('DIR');
It is also possible to interact with the results, within a for loop:
for (select linenum ilinenum, line iline from SYSTEM ('DIR'))
do
write ilinenum || chr(9) || iline;
end
A few things to note:
No interaction is possible, e.g. if you attempt to COPY a file and are prompted if you want to overwrite Y/N, this will get stuck.
The IP21 Service Account will always execute the command, regardless of the user who executes the SQLPlus query.
If you wish to execute multiple commands, you will need to load a batch file

SQL*Plus ## behaviour with spaces in the parent script path

I have to
Connect
Call sql-script
Exit sqlplus
So, I've created a sql-script run.sql:
set def on
WHENEVER SQLERROR EXIT ROLLBACK
conn &2
#"&1";
exit;
For example, I have some sql-scripts in a folder "c:\folder with spaces\":
a) install.sql with contents:
prompt Hello
##a.sql
##b.sql
b) and of course I have a.sql and b.sql files in the folder with some actions.
So, I try to run my script:
sqlplus /nolog #"Path_to_Run\run.sql" \"c:\folder with spaces\install.sql\" user/pass#server
And I got an output:
Hello
SP2-0310: unable to open file "c:\folder.sql"
So, it can open install.sql, but cannot open ##a.sql. He tries to run such script:
#c:\folder with spaces\a.sql
But how can I place quotes here? He should run the script a.sql inside the folder "c:\folder with spaces\".
You could split your parameters so that run.sql contains
set def on
WHENEVER SQLERROR EXIT ROLLBACK
conn &2
host cd "#3"
#"&1";
exit;
And invoke it with
sqlplus /nolog #"Path_to_Run\run.sql" \"c:\folder with spaces\" install.sql user/pass#server
Note that it might need some tuning, it does not work on my computer because of cygwin messing with its own cd.
Thanks to Alex Poole, the answer is in documentation
SP2-0310 Error when Calling a SQL Script in Another Script using ## and the Path to the Parent Script Contains Spaces (Doc ID 745780.1)
And there are three solutions:
Use the 8dot3 notation. E.g. Use PROGRA~1 instead of "Program Files".
Avoid saving the sql scripts on a path containing spaces in the folder names.
cd to the directory containing the script instead of providing the full path when invoking the script in SQL*Plus.
First method doesn't work for me in Windows 7 (there are a lot of work to set up 8dot3 in Win7). I get:
>cd c:\folder~1
The system cannot find the path specified.
Second method doesn't work for me too, I cannot operate these folders. I have to use them as is.
And the third method works fine (but in another way than in the previous message):
cd "c:\folder with spaces"
sqlplus /nolog #c:\run\run.sql install.sql user/pass#server
So, you have to change dir before sqlplus, host cd doesn't work.
Thank you, guys.

Oracle spool location

I'm using SQL Developer to spool the DDL for some packages, but I cannot identify the location where the server is spooling. Oracle is installed on AIX, but I don't know the user it's using to connect to the OS.
Basically, when I run:
spool test.lst
select 1 from dual;
spool off
I get a confirmation message in console
1
----------------------
1
and whenever I try to give a path, I'd get an error, probably because of user rights:
spool /tmp/test.lst
select 1 from dual;
spool off
Cannot create SPOOL file /tmp/test.lst
1
----------------------
1
So my question is where is test.lst?
Spool is a client activity, not a server one; the .lst file will be created on the machine that SQL Developer is on, not the server where the database it's connecting to resides.
Under Windows 7 and SQL Developer 3.1, by default for me that seems to store the .lst in %APPDATA%\Sql Developer\
You can spool to a specific directory, e.g. spool c:\windows\temp\test.lst, and if you have it set up can use something like spool \\<aix-server>\<dir>\test.lst. Paths vary according to your client OS, of course.
Under my LINUX installation the default spool directory is from where you are running sqlplus.
ie. If you run sqlplus from a directory where you do not have write permissions it will fail.
Try typing "!" or "host" at the sqlplus prompt to goto the OS.
pwd the directory end up in and I guess this is location of your spool.
(type exit to go back to sqlplus)
What the other guy said for the default location was true for oracle 10g but since I am using the latest oracle 21c(at the time of writing this answer) here is the solution that worked for me.
in case if you created a spool file lets say: spool on; spool tempfile;
then the default location would be at the place you installed the oracle db software which in my case is
D:\WINDOWS.X64_213000_db_home\bin
and then in the
bin
folder the file would be created as
tempfile.LST
and in order to open a
.lst
file just use the default notepad or any such editors to open your output
However here is a better suggestion by me:
type spool on; SPOOL C:/path/xyz.txt; #here change your directory,the pathname and file name for query file
and most importantly DO NOT FORGET TO TYPE
SPOOL OFF;
at the end of the query. In case if you forget then your file wont be created and you will lose all your progress of your saved output. and lemme give you another pro tip: and that is to not write the same spoolfile name otherwise it will be overwritten

How to run PL/SQL program in Oracle10g for Linux

BEGIN
dbms_output.put_line('Welcome to PL/SQL');
END;
/
I have this code in sample.sql file.
How to run sample.sql?
You can run it using SQL*Plus (using your username/password and the name of your instance):
sqlplus username/password#instance # sample.sql
Another way would be to download free SQL Developer from Oracle, open and execute the file there.
Note that the text will not be displayed per default, you need to enable the output before.
Put the following line into your file as first line:
SET SERVEROUTPUT ON

Resources