Sub topic 15
Running SQLcl in Scripts and Automation
Assume you know basic SQL, can use a terminal, and have SQLcl installed. You also need access to an Oracle Database and an authentication method that does not expose a password in the script.
SQLcl can run commands interactively, or it can read commands from a script file. Non-interactive execution is useful for reports, deployment steps, scheduled jobs, and continuous integration pipelines.
Run a SQLcl script
A SQLcl script is a text file containing SQL and SQLcl commands. For example, report.sql might contain:
set heading on
set feedback on
select employee_id, first_name, last_name, salary
from employees
order by employee_id;
exitRun the script by placing its name after the connection information:
sql hr@//dbhost.example.com:1521/orclpdb1.example.com @report.sqlThe @report.sql argument tells SQLcl to read and execute that file. The script should usually end with exit, especially in automation. Without it, SQLcl might remain open after the last SQL statement.
Use silent and limited-login modes
The -S option starts SQLcl in silent mode. It suppresses the startup banner and reduces extra output. The -L option limits the login attempt to one try. This prevents an automated job from waiting forever after a failed connection.
sql -S -L hr@orclpdb @report.sqlThis command uses the orclpdb connect identifier and runs report.sql. The identifier can be an Easy Connect string, a TNS alias, or another connection name supported by your configuration.
Silent mode does not hide errors from the SQL statements themselves. Capture standard output and standard error in your scheduler or shell so that failures can be logged and detected.
Keep credentials out of scripts
Do not place a database password in report.sql, source control, or a command line that other users can inspect. Command-line passwords can appear in shell history or process listings.
Prefer one of these approaches:
- Use a wallet or another configured external authentication method.
- Use a saved SQLcl connection whose credential is protected by the local connection store.
- Let an approved job runner provide credentials through its secret-management system.
- Connect interactively when running a command manually, so SQLcl prompts for the password.
A script should contain database work, not secret values.
Understand the automation flow
A successful connection does not guarantee that every SQL statement succeeded. Test both the connection and the script result. Your scheduler should treat a nonzero process status or an error message as a failed job.
Make scripts repeatable
Automation often runs the same script many times. Avoid commands that depend on previous session state. Set important SQLcl options inside the script, use explicit schema names when needed, and end with exit.
For a report, redirect output from the shell if the job needs a file:
sql -S -L hr@orclpdb @report.sql > report-output.txtCheck that the output file does not contain credentials or sensitive data before sending it to another system.
Exercise
Why should an automated SQLcl command use both -S and -L?
Show answer
-S reduces interactive startup output, while -L limits login attempts so a failed login does not wait indefinitely for another prompt.