-1

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.

7
  • stackoverflow.com/questions/5079180/… Commented Jul 31 at 13:50
  • 3
    It seems like datapatch is a cmd or bat file? To resolve your problem use call datapatch ... Commented Jul 31 at 14:31
  • 2
    set PATCH_ERROR vs %PATCH_EXIT_CODE%. This means you have an if command as if NEQ 0 (...) else (...), rather than if %PATCH_ERROR% NEQ 0 (...) else (...). Commented Jul 31 at 14:34
  • 1
    Or if that does not work, try cmd.exe /s/c "datapatch --verbose" >> ... . Commented Jul 31 at 14:40
  • 1
    Also you don't appear to have defined %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 ). Commented Jul 31 at 14:43

1 Answer 1

1

Out of courtesy, and because the comment section would not be appropriate for such things, here's an example of one way I might have chosen to do this.

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion

    Rem Define env variables
    Set "ORACLE_HOME=E:\Oracle\19e\db1a"
    Set "ORACLE_SID=orcl"
    Set "OPATCH_DIR=%ORACLE_HOME%\OPatch"
    Set "SCRIPT_DIR=E:\scripts"
    Set "LOG_FILE_POST_REFRESH=%SCRIPT_DIR%\logs\post_refresh.log"

    Rem Check for datapatch existence.
    If Not Exist "%OPATCH_DIR%\datapatch.bat" (
        Echo datapatch not found.
        Pause
        Exit /B 1
    )

    Rem Change to OPatch directory
    CD /D "%OPATCH_DIR%"

    SetLocal EnableDelayedExpansion
    
        (
            Echo !DATE! !TIME!
            Echo Starting post refresh ...

            Rem Apply the latest patch on the PDB
            Echo Applying the latest patch set ...
            Call "datapatch.bat" -verbose

            Rem Prevent script from exiting by checking errorlevel immediately after.
            Set "PATCH_EXIT_CODE=!ERRORLEVEL!"

            Rem Handle outcome
            (
                If !PATCH_EXIT_CODE! Equ 0 (
                    Echo [!DATE! !TIME!] Datapatch completed successfully.
                ) Else (
                    Echo [!DATE! !TIME!] ERROR: datapatch failed with exit code !PATCH_EXIT_CODE!.
                    Echo Check the log file at %LOG_FILE_POST_REFRESH% for details.
                )
                Pause
            ) 1>CON
        ) 1>"%LOG_FILE_POST_REFRESH%" 2>&1
    
    EndLocal

Exit /B 0

Important Note: I need to mention however, that in order for the exit code to be returned correctly, datapatch.bat would need to be handling, then returning that to this calling script. This script cannot itself do that without that functionality built into that other script.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for taking the time and the effort to re write an elegant version of the script.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.