How to Execute Oracle SQL Script and Auto-Exit SQL*Plus from Windows Command Prompt


3 views

When automating database tasks, developers often need to execute SQL scripts through SQL*Plus without manual intervention. The key pain point emerges when the script lacks an EXIT command, leaving the SQL*Plus session open and blocking subsequent commands in the batch process.

The most efficient approach uses command-line parameters to both execute the script and force termination:

sqlplus username/password@database @script.sql EXIT

Alternatively, you can use this more explicit syntax:

sqlplus -L username/password@database @script.sql EXIT

The -L flag attempts only one login (useful for scripts). The EXIT command after the script reference ensures termination.

For complex scenarios, you might need to handle return codes:

@echo off
sqlplus -S username/password@database @script.sql EXIT > output.log 2>&1
if %ERRORLEVEL% neq 0 (
    echo SQL*Plus execution failed
    exit /b 1
)

If you need to pass parameters to both SQL*Plus and your script:

sqlplus username/password@database @script.sql param1 param2 EXIT

For silent execution (no banner output):

sqlplus -S username/password@database @script.sql EXIT > nul
  • Ensure the EXIT command comes after the script reference
  • Use -L for scripts that might encounter login failures
  • Redirect output (> output.log) when running in scheduled tasks
  • The EXIT command will commit any pending transactions

When automating Oracle database tasks through command-line scripts, a common pain point arises: sqlplus.exe doesn't automatically exit after script execution unless explicitly told to do so. This becomes problematic when:

  • Running batch processes that chain multiple database operations
  • Scheduling jobs through Windows Task Scheduler
  • Integrating with CI/CD pipelines

Oracle's SQL*Plus accepts several command-line arguments that solve this elegantly without modifying your SQL scripts:

sqlplus username/password@database @script.sql exit

Or more securely (to avoid password exposure):

sqlplus /nolog @run_script.sql

Where run_script.sql contains:

CONNECT username/password@database
@your_actual_script.sql
EXIT

For production environments, consider these robust approaches:

REM Windows batch example with error handling
sqlplus -L -S username/password@database @script.sql & exit_code.txt
IF %ERRORLEVEL% NEQ 0 (
    ECHO SQL*Plus error occurred
    TYPE exit_code.txt
    EXIT /B 1
)

Using PowerShell for better control:

$sqlOutput = sqlplus -S username/password@database @script.sql | Out-String
if ($LASTEXITCODE -ne 0) {
    Write-Error "SQL*Plus execution failed"
    $sqlOutput
    exit 1
}

Proper exit handling enables:

  • Accurate job status reporting
  • Clean resource release
  • Seamless integration with other tools
  • Proper error code propagation

If you encounter problems:

  1. Verify the -S (silent) flag isn't suppressing critical errors
  2. Check Oracle environment variables (ORACLE_HOME, PATH)
  3. Test connection strings independently first
  4. Review SQL*Plus version compatibility