In the below batch script, the script exits right after the datapatch command.
The datapatch command executes successfully but none of the code in the if/else clause is executed.
:: Define env variables
set ORACLE_HOME=E:\Oracle\19e\db1a
set ORACLE_SID=orcl
set SCRIPT_DIR=E:\scripts
set LOG_FILE_POST_REFRESH=%SCRIPT_DIR%\logs\post_refresh.log
@echo %date% %time% > %LOG_FILE_POST_REFRESH%
@echo Starting post refresh ... >> %LOG_FILE_POST_REFRESH%
:: Apply the latest patch on the PDB
@echo Applying the latest patch set ... >> %LOG_FILE_POST_REFRESH%
:: Change to OPatch directory
cd /d %ORACLE_HOME%\OPatch
datapatch -verbose >> %LOG_FILE_POST_REFRESH% 2>&1
:: Prevent script from exiting by checking errorlevel immediately after
set PATCH_ERROR=%ERRORLEVEL%
:: Handle outcome
if %PATCH_EXIT_CODE% NEQ 0 (
echo [%date% %time%] ERROR: datapatch failed with exit code %PATCH_EXIT_CODE%. >> %LOG_FILE%
echo Check the log file at %LOG_FILE% for details.
) else (
echo [%date% %time%] Datapatch completed successfully. >> %LOG_FILE%
)
exit 0
A snippet of the output is as follows.
Adding patches to installation queue and performing prereq checks...
done
Installation queue:
For the following PDBs: CDB$ROOT PDB$SEED PDB1 PDB2 PDB3
No interim patches need to be rolled back
No release update patches need to be installed
No interim patches need to be applied
SQL Patching tool complete on Thu Jul 31 09:30:19 2025
Any suggestions as how to prevent the script from exiting?
Thanks in advance.
datapatchis acmdorbatfile? To resolve your problem usecall datapatch ...set PATCH_ERRORvs%PATCH_EXIT_CODE%. This means you have anifcommand asif NEQ 0 (...) else (...), rather thanif %PATCH_ERROR% NEQ 0 (...) else (...).cmd.exe /s/c "datapatch --verbose" >> ....%LOG_FILE%, only%LOG_FILE_POST_REFRESH%. If I'm brutally honest, the only lines I wouldn't have to change from your current submission would be this one) else (, and this one).