hi i tried to take schema backup in oracle running the below command in c prompt
D:\oracle11g\product\11.2.0\dbhome_1\BIN>expdp system/password schemas='XYZ' compress=Y directory=expdp_dir dumpfile='expdp_dumpfile_%T.dmp' logfile='c:\dumpfile_exp.log
and it worked fine.
But when i tried to put the above command in bat file it is not working
i kept the below code in bat file.
#ECHO OFF
D:\Oracle11g\product\11.2.0\dbhome_1\BIN expdp system/password schemas='XYZ' compress=Y directory=expdp_dir dumpfile='expdp_dumpfile_%T.dmp' logfile='c:\dumpfile_exp.log
if %ERRORLEVEL%==0 (
echo Success
)
if %ERRORLEVEL%==2 (
echo Failed
)
i am getting a message as system could not the path and success message.
please help me out.
Your code:
#ECHO OFF
D:\Oracle11g\product\11.2.0\dbhome_1\BIN expdp system/password schemas='XYZ' compress=Y directory=expdp_dir dumpfile='expdp_dumpfile_%T.dmp' logfile='c:\dumpfile_exp.log
if %ERRORLEVEL%==0 (
echo Success
)
if %ERRORLEVEL%==2 (
echo Failed
)
What I would use:
#Echo off
D:\Oracle11g\product\11.2.0\dbhome_1\BIN\expdp.exe system/password schemas='XYZ' compress=Y directory=expdp_dir dumpfile='expdp_dumpfile_%T.dmp' logfile='c:\dumpfile_exp.log
if %ERRORLEVEL%==0 (echo "Success") else (echo "Failed with error code: " %ERRORLEVEL%)
Notice the 2nd line, you put:
D:\Oracle11g\product\11.2.0\dbhome_1\BIN expdp system/password schemas='XYZ' compress=Y directory=expdp_dir dumpfile='expdp_dumpfile_%T.dmp' logfile='c:\dumpfile_exp.log
Notice the second line where I put:
D:\Oracle11g\product\11.2.0\dbhome_1\BIN\expdp.exe system/password schemas='XYZ' compress=Y directory=expdp_dir dumpfile='expdp_dumpfile_%T.dmp' logfile='c:\dumpfile_exp.log
Related
I want to develop a batch script which will execute each .sql SQL scripts present in the folder where the batch file is placed to and record the logs to a filename_sqloutput.txt file .
The condition is : If any script gives any error message like column name incorrect , or table name etc. The script execution should stop immediately and further scripts should not executed .
I tried with the below code: But its not working as even if the script is giving errors in the output file . The script execution is not stopping ..
Need your help !!!
#echo off
for /f %%a IN ('dir /b *.sql') do (call sqlcmd -S AMRVSP000000318 -i %%a -o"%%~na_sqloutput.txt"
findstr "Msg" %%~na_sqloutput.txt >nul & if %errorlevel% EQU 1 (exit) else (echo Successfully executed %%~na_sqloutput.txt)
)
pause
FINDSTR will set %ERRORLEVEL% as follows:
0 (False) a match is found in at least one line of at least one file.
1 (True) if a match is not found in any line of any file, (or if the file is not found at all).
2 Wrong syntax
An invalid switch will only print an error message in error stream.
Moreover, follow EnableDelayedExpansion article and rewrite the script to more readable structure:
#echo off
SETLOCAL EnableExtensions EnableDelayedExpansion
for /f %%a IN ('dir /b *.sql') do (
call sqlcmd -S AMRVSP000000318 -i %%a -o"%%~na_sqloutput.txt"
findstr "Msg" "%%~na_sqloutput.txt" 2>NUL
if !errorlevel! EQU 0 (
echo error occured %%~na_sqloutput.txt
rem pause to see error
pause
exit /B
) else (
echo Successfully executed %%~na_sqloutput.txt
)
)
pause
.... if errorlevel 1 (exit...
%errorlevel% is the initial value of errorlevel as it stands when the for is encountered.
See endless SO articles about using delayed expansion as another solution.
This syntax means "if the current errorlevel is 1 or greater dothis else dothat"
I am using a batch file my.bat as given below:
#echo off
setlocal enabledelayedexpansion
for /F "tokens=*" %%A in (user.txt) do (
sqlplus -s -l %%A #fetch.sql
) >> output.txt
where user.txt (list of all user details for which I need expiry date. this list may have around 40-50 rows) is:
dbuser/password#database1
readuser/p#ssw0rd#database1
adminuser/Pa$$word#database2
.......
.......
.......
and fetch.sql is:
set pagesize 200
set linesize 200
select username, expiry_date from user_users;
exit;
The problem I am facing is, script my.bat captures here all the required details in output.txt along with SQL ERRORS (ORA-01017: invalid username/password; logon denied ,ORA-12154: TNS:could not resolve the connect identifier specified, ORA-28000: the account is locked ) but its getting hanged at the point whenever it encounteres below error message :
ERROR:
ORA-28001: the password has expired
Changing password for readuser
Please can you let me know how I can ignore this ERROR message too and keep my script running further ?
You can have whenever sqlerror exit failure in your SQL script, but because you only run that after a successful connection it won't catch this error.
You could instead launch SQL*Plus without logging in, using the /nolog switch, and then connect explicitly:
#echo off
setlocal enabledelayedexpansion
for /F "tokens=*" %%A in (user.txt) do (
(
echo whenever sqlerror exit failure
echo connect %%A
echo #fetch.sql
) | sqlplus -s /nolog
) >> output.txt
This also means your credentials aren't supplied on the command line; not sure about Windows so much but on other platforms that is a considerable security risk. Of course you still have them stored in a plain text file which itself isn't very secure, so that's more of a general point.
You could put your fetch.sql statements directly in the batch file if you prefer, by echoing those instead of the # start command; same effect but one less file to maintain.
First of all i am a noob in programming.
I am trying to make a batch file which takes an installed directory of a program as user input when run for the first time (means it should not ask for the directory the second time it is run). By searching for various scripts, i reached till here,
#echo off
Echo =============================================
echo Directory
Echo =============================================
setlocal enableextensions enabledelayedexpansion
set /p mypath=Please specify install directory;
Echo %mypath% ----was what you typed
pause
echo start>temp.txt
echo %mypath%>>temp.txt
echo \programfolder\program.exe>>temp.txt
echo -argument -argument>>temp.txt
setlocal enabledelayedexpansion
set FINAL=
for /f %%a in (temp.txt) do (
set FINAL=!FINAL!%%a
)
echo %FINAL%>input.txt
del /q temp.txt
Pause
start "<input.txt"
This saves the input path in the "input.txt" text file, and runs the program the next time it is launched.
I want the text file to have the saved path as "start driveletter:\foldername\foldername with spaces\programfolder\program.exe" -arguments
However the "start", "program folder", "program.exe" and "-arguments" are fixed.
The user input path should get saved in- %mypath%.
The does what you asked, I think:
#echo off
if exist "go.bat" go.bat
set /p "mypath=Please specify install directory; "
Echo "%mypath%" ----was what you typed
pause
>"go.bat" echo #start "" "%mypath%\programfolder\program.exe" -argument -argument
I would like to use a single batch file to execute multiple sql in a sequence..
IE: sql2 script calls a table created by sql1 script.. etc..
here is the batch code I have so far..
it works to run a single sql file but I need it to run the first one and then then next..
thanks in advance.
#ECHO OFF
echo.
echo.
SET /P uname=Username:
echo.
echo.
SET /P pass=Password:
echo.
echo.
SET /P mydatabase=Database:
echo.
echo.
set oracle_sid=ins
sqlplus -s %uname%/%pass%#%mydatabase% #J:/A/scripts/_TABLES/Table1_.sql \n
sqlplus -s %uname%/%pass%#%mydatabase% #J:/A/scripts/_TABLES/Table2_.sql \n
sqlplus -s %uname%/%pass%#%mydatabase% #J:/A/scripts/_TABLES/Table3_.sql \n
sqlplus -s %uname%/%pass%#%mydatabase% #J:/A/scripts/_TABLES/Table4_.sql \n
sqlplus -s %uname%/%pass%#%mydatabase% #J:/A/scripts/_TABLES/Table5_.sql \n
sqlplus -s %uname%/%pass%#%mydatabase% #J:/A/scripts/_TABLES/Table6_.sql \n
sqlplus exit
pause
make a 'driver' sql script.
inside it would look similar to this:
#Table1_.sql
#Table2_.sql
#Table3_.sql
#Table4_.sql
then just call this one once from the OS
Or you can run them all together.
cat Table1_.sql > /tmp/temp.sql
cat Table1_.sql >> /tmp/temp.sql
cat Table1_.sql >> /tmp/temp.sql
sqlplus USERNAME/PASSWORD#DOMAIN #/tmp/temp.sql
I am just starting to learn how to script. I'm trying to understand how the system handles Error Levels and how they can be used in error handling. I know there is a difference between the environment variable %ERRORLEVEL% and the Error Level of the system. If I understand this correctly, then the
If ERRORLEVEL 1
code would check the environment variable before it checks the error level of the previous command.
So, in my program I am trying to interface a startup/stop script that will start/stop all scripts of a given machine (for testing I'm just using one application notepad.exe as an example). I have two wrapper scripts that will either start up or stop the applications by passing arguments to the independent script. If there is an error in the independent script, it will set the errorlevel using the
EXIT /B n
command. Once control is returned to the calling script, it will go to an error handling script if the exit status is non-zero.
At first I was setting the %ERRORLEVEL% to zero manually and then testing for an error after a START or TASKKILL command. But then I read that clearing %ERRORLEVEL% with
SET ERRORLEVEL=
is a better method. My issue comes in when I try to start the app with
START "" notepad.exe
Whenever I test the errorlevel after this command it is always greater than or equal to 1 unless I use SET ERRORLEVEL=0 before I run the start command. I have inserted the code for the four scripts below. Any insight and advice would be greatly appreciated.
appstart.bat:
#echo off
:: Script for application Start
set ERRORLEVEL=
:: ****
:: Additional Batch files will be executed from within this file
:: Example:
:: Call Appbat01.bat
:: The called batch file should set ERRORLEVEL non-zero if error
:: ****
call test.bat -start
if ERRORLEVEL 1 (call error.bat)
echo.
echo Control was returned to appstart.bat...
:: **** End Calls
goto end
:end
appstop.bat:
#echo off
:: Script for application Start
set ERRORLEVEL=
:: ****
:: Additional Batch files will be executed from within this file
:: Example:
:: Call Appbat01.ba
:: The called batch file should set ERRORLEVEL non-zero if error
:: ****
call test.bat -stop
if ERRORLEVEL 1 (call error.bat)
echo.
echo Control was returned to appstop.bat...
:: **** End Calls
goto end
:end
test.bat:
#echo off
if "%1"=="-start" goto :start
if "%1"=="-stop" goto :stop
goto wrongParams
:start
::****
:: Insert start up stripts here...
:: If there is an error, set ERRORLEVEL=1
::****
set ERRORLEVEL=0
echo.
echo ********
echo starting the service...
echo.
::start "" "C:\Program Files\Microsoft Office\office11\winword.exe"
start notepad.exe
if ERRORLEVEL 1 goto error
qprocess notepad.exe
echo *Start.success* ERRORLEVEL is: %ERRORLEVEL%
echo.
goto end
:stop
::****
:: Insert stopping stripts here...
:: If there is an error, set ERRORLEVEL>1
::****
set ERRORLEVEL=0
echo.
echo ********
echo stopping the service...
echo.
qprocess notepad.exe
taskkill /f /im notepad.exe
if ERRORLEVEL 1 goto noProcess
goto end
:noProcess
set ERRORLEVEL=2
echo *noProcess* ERRORLEVEL is now: %ERRORLEVEL%
echo.
exit /b 2
:error
:: Errorhandler. Log application status and cause of error here. Set
:: ERRORLEVEL > 1 before returning to caller.
set ERRORLEVEL=1
echo.
echo **** Error handler inside test.bat ****
echo.
echo *error* ERRORLEVEL is now: %ERRORLEVEL%
echo.
exit /b 1
:wrongParams
:: Output an error if the wrong parameters were passed to this script.
:: Maybe try to self correct the parameter...
set ERRORLEVEL=1
echo.
echo '%1' is an invalid parameter.
echo Usage: %0 [-stop ^| -start]
echo *wrongParams* ERRORLEVEL is now: %ERRORLEVEL%
echo.
exit /b 1
:end
error.bat:
#echo off
echo **** You have reached error.bat ****
echo ERRORLEVEL inside of error.bat is: %ERRORLEVEL%
echo.
::*** Handle error...***
goto error%ERRORLEVEL%
:error2
echo The process could not be stopped for some reason.
goto end
:error1
echo The process had an error in start up.
::*** ***
goto end
:end
You should never SET the %errorlevel% variable. You are correct that there is a difference; The errorlevel that you get from an exiting process is an internal register that you can read with the %errorlevel% syntax. However, if you create a variable named ERRORLEVEL, it will mask the internal register and you lose access to the exit codes.
If you need to set the errorlevel register to a specific value, you can do it with the following command:
%comspec% /c exit %value%
This will spawn a process which immediately exits with the desired code.