Oracle Script File Execution in *.bat file not completing - oracle

I have the following *.bat file executing itself on a schedule. This file consists of different tasks, primarily involves in having a the backup dump file exported to the Test DB Machine every day.
#echo off
#echo %time%
del C:\Factory\index.sql
echo exporting indexes...
Imp jksbschema/password file=C:\DBDUMP\jksb.dmp full=yes log=C:\Factory\log\dump_index.log indexfile=index.sql
#echo %time%
echo connecting to Oracle...
echo Delete and Re-Creation of User
sqlplus "sys/password as sysdba" #C:\Factory\script\step_1.sql
echo importing dump file begins...
#echo %time%
Imp jksbschema/password file=C:\DBDUMP\jksb.dmp full=yes log=C:\Factory\log\dump.log Compile=n INDEXES=n
echo Applying security priviledges
#echo %time%
sqlplus "sys/password as sysdba" #C:\Factory\script\step_2.sql
#echo %time%
ssr 0 "CONNECT" "REM CONNECT" C:\Factory\index.sql
echo Loading Indexes....`enter code here`
sqlplus "jksbschema/password as sysdba" #C:\Factory\index.sql
#echo %time%
exit
shutdown.exe -r -t 00
Since its a long operation, I execute itself on every night, expecting it to be ready next morning. I also include a command restart the machine once the entire process is over. All the command finishes without asking an input expect the following command:
sqlplus "jksbschema/password as sysdba" #C:\Factory\index.sql
after the above command executes, the batch file asks for an input to exit the process as the snapshot below says
Can anybody help in automatically finish the job and restart itself, rather than ask for an input after the index.sql file is loaded?

Create a file named quit.txt, inside it put the text "quit".
Where you see the lines that execute sqlplus, like this one:
sqlplus "jksbschema/password as sysdba" #C:\Factory\index.sql
change them to this:
sqlplus "jksbschema/password as sysdba" #C:\Factory\index.sql < quit.txt

Old stuff, but however: Let "exit;" be the last command of your SQL script. This tells sqlplus to quit and finish execution.

Related

Accept command does not pause when SQL script executed from a window batch file

While running the following batch file, it executes the sql script and then exit. Here is the content of
runsql.bat
echo exit | sqlplus <userAuthString> #scriptWithAccept.sql
scriptWithAccept.sql
ACCEPT Continue CHAR PROMPT 'Press <CTRL>+C to cancel or any other key to continue.'
The problem is that batch does not pause to accept the user input provided in the Sql script. It pauses if I run the sql file from sqlplus window. I also tried using the following
start sqlplus <userAuthString> #scriptWithAccept.sql
this opens a new window and pauses for accept but it does not exit the sqlplus command window. This can be fixed by adding the exit to the end of the sql file but I don't want the exit command inside the sql file.

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

Windows batch script output not written to log

We have this Windows batch script:
call commands/do-work.cmd | tee my.log
The do-work.cmd includes
impdp user/pw#db directory=mydir dumpfile=my.dmp logfile=logdir:imp.log schemas=a,b,c,c parallel=6
(
echo my.sql
echo exit
) | sqlplus user/pw#db
call mvn clean install
Of these commands the output from sqlplus and mvn is written to my.log but the output of impdp is not. How can I get impdp output into my.log?
Tried using "call" ahead of impdp but the impdp command choked for some reason... complaining about log not found.
Any ideas?
I don't have IMPDP in order to test it how it works. So, "logfile=logdir:imp.log" generates a imp.log file, right? you want the content of this file inside of MY.LOG?
Try:
TYPE imp.log >> my.log
If IMPDP write the info in the console you can try adding ">> my.log" at the end of the command line instead.

Execute SQL Plus SQL command from batch-cmd window hangs

I am trying to execute a PL-SQL statemtn in a file and log the results to a log file. It works, but the DOS window hangs and I have to manually close it.
Am I ending the SqlPlus session incorrectly?
rem SET TERMOUT OFF;
SET SqlPlusExe=C:\Oracle\product\11203_32bit\CLIENT_1\bin\sqlplus.exe
SET MyUser=MyID
SET MyPassword=MyPwd
SET Host=RXODSDEV.CIGNA.COM
SET SqlFile=C:\Users\MyLanId\Desktop\SQLPlus\test.sql
SET LogFile=C:\Users\MyLanId\Desktop\SQLPlus\test.log
%SqlPlusExe% %MyUser%/%MyPassword%#%Host% #%SqlFile% >> %LogFile%
quit;
/
For Windows-scripting I recommend you to set the environment variable "LOCAL" ("TWO_TASK" on linux). The variable will specify you connection string.
set LOCAL=RXODSDEV.CIGNA.COM
REM Create the file to run
#(
echo connect %MyUser%/%MyPassword%
echo #%SqlFile%
echo exit^;
) > %TEMP%\run.sql
sqlplus /nolog #%TEMP%\run.sql >> out.log
But who is doing DOS scripting these days? Powershell is so much more fun!
Maybe you find my launcher script useful, take a look at http://www.unix.com/windows-and-dos-issues-and-discussions/256021-windowss-batch-launcher-oracle-sql-linux-sh-scripts-available-here.html

Using Batch Script connect to oracle db run a select command and output all data to a flat file

I am trying to create a batch file for windows (XP) which will have several sqls and when its run it would
1. connect with oracle
2. set the userid/password/schema
3. run each sql in the loop and
4. output each sql outputs to its own flat file.
I have started the script
#ECHO off
SET STATE=fl
TABLE1=AGENCY
set SQL1="SELECT Column_ID||CHR(31)||column_ENTITY_CD||CHR(31) FROM AGENCY"
set TABLE2=FIRM
set SQL2="SELECT Column_ID||CHR(31)||Column_NM||CHR(31) FROM FIRM"
set TABLE3=FL_CO_LOB
Set SQL3="SELECT Column_ID||CHR(31)||Column_LOB_CODE||CHR(31) FROM FL_CO_LOB"
...
SET NumberOfTables=19
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /l %%A IN (1,1,%NumberOfTables%) DO (
echo !SQL%%A!
)
endlocal
I can get the SQL out of the variable but don't have any clue how to connect to oracle and run the sql and get the output to a defined file.
please give me some direction.
one thing to notice that the echo is printing including the double quote. but If i don't have them then it just print the 1st word not the whole query.
Thanks
If you've installed the oracle client on your workstation, you have SQLPlus.exe for command line work with the database. Your TNSNAMES.ORA file needs to be up to date, and tnsping needs to be able to locate your service.
I think you'll need to change your approach. The scripts you want to run will need to be fed to the SQLPlus.exe program, rather than being delivered semi-interactively.
I suggest creating a .sql script instead of creating evnironment variables
Your batch file line might look like:
#ECHO off
SET STATE=fl
set TABLE1=AGENCY
set TABLE2=FIRM
echo SELECT Column_ID^|^|CHR(31)^|^|column_ENTITY_CD^|^|CHR(31) FROM %TABLE1% > tablecommands.sql
echo SELECT Column_ID^|^|CHR(31)^|^|Column_NM^|^|CHR(31) FROM %TABLE2% >> tablecommands.sql
echo SELECT Column_ID^|^|CHR(31)^|^|Column_LOB_CODE^|^|CHR(31) FROM FL_CO_LOB >> tablecommands.sql
...
<until your 19 SQL statements are declared>
SQLPlus User/Password#Database #tablecommands.sql
Add the sqlplus login command before your sql commands in your batch file. It executes and writes to logs file where the batch file resides
syntax : sqlplus -s ora_user_name/ora_user_password [as sysdba]#ora_sid#"path_to_sql_file" > output.log
ex. sqlplus -s scott/tiger#xe #"D:\Oralcle\scripts\sql_file.sql" > output.txt

Resources