Introduction 

The SQLPLUSJob JITL Job Template provides a standardized interface for executing Oracle® SQL*Plus scripts. The job template offers out-of-the-box capabilities for:

  • executing an SQL*Plus script,
  • passing arguments to an SQL*Plus script,
  • collecting results,
  • passing results as Order Variables to next jobs,
  • using a JS7 - Credential Store.

Usage

When defining the job either:

  • invoke the Wizard that is available from the job properties tab in the Configuration view and select the JITL SQLPLUSJob and relevant arguments from the Wizard,

or

  • specify the JITL job class and com.sos.jitl.jobs.db.oracle.SQLPLUSJob Java class name and add arguments as explained in the documentation below.

Example

Download: dbSQLPlusExecution.json

The following example shows the basic use of the SQLPLUSJob. It executes SQL*Plus code from an .sql file and writes output to the stdout channel.


The job arguments can be specified as follows:


Explanation:

  • The command_script_file argument points to a file with, for example, the following SQL code:
    • WHENEVER SQLERROR EXIT SQL.SQLCODE
      WHENEVER OSERROR EXIT FAILURE
      SELECT 'SET business_date IS ' || TO_CHAR(SYSDATE,'dd.mm.yyyy DAY') FROM DUAL;

Documentation

The Job Documentation including the full list of arguments can be found under: https://www.sos-berlin.com/doc/JS7-JITL/SQLPLUSJob.xml

The SQLPLUSJob class accepts the following arguments:


Name

Purpose

Required

Default Value

Example

command_script_file

Path to the SQL script that should be executed

true


c:\app\bin\sqls\get_upd_count.sql

db_url

Database service name or instance name

true


DORCL01

db_user

User name for database access

true


scott

db_password

Password for database access

true


tiger
shell_commandPath to the SQL*Plus command line utilitytrue
sqlplus

variable_parser_reg_expr

Regular expression to parse output and to set order variables for subsequent jobs

false^SETs+(\\s)\\s*ISs(.*)$



command_line_optionsSQL*Plus command line optionsfalse
-S -L
ignore_ora_messagesIgnore Oracle error messagesfalse

ignore_sp2_messagesIgnore sp2 error messagesfalse

include_filesCode blocks that should be included before executing the SQL*Plus scriptfalse

credential_store_fileLocation of a credential store database (*.kdbx)false
./config/private/jobs.kdbx
credential_store_keyLocation of a credential store key file (*.key)false
./config/private/jobs.key


The SQLPLUSJob can be used with a credential store to hold sensitive arguments. For use of the credential_store_* arguments see JS7 - Use of Credential Store with JITL Jobs.

Argument: command_script_file

This argument expects a path to the SQL*Plus script that holds the SQL statements to be executed.

Argument: db_url

The db_url argument holds the Oracle service name or instance name.

Argument: db_user

The database user account for which the SQL*Plus script is executed.

Argument: db_password

The password for the database user account specified with the db_user argument.

Argument: shell_command

The value of this variable specifies the location of the SQL*Plus command line utility. The SQL*Plus script will be processed by the indicated SQL*Plus binary.

Argument: variable_parser_reg_expr

This argument specifies a regular expression for parsing the output created by the the DBMS_OUTPUT package from the PL/SQL statements. For each match an order variable is created that is available for subsequent jobs. For example, the statement

EXEC DBMS_OUTPUT.PUT_LINE( 'SET order_date IS ' || TO_CHAR(CURRENT_DATE, 'YYYY-MM-DD') );

writes the following output to the console:

SET order_date is 2021-05-04

This output will be parsed by the regular expression

^SETs+(\\s)\\s*ISs(.*)$

that will result in an order variable

order_date = "2021-05-04"

Subsequent instructions and jobs can use of the $order_date order variable.

Argument: command_line_options

 The value of this variable specifies the command line parameters required by SQL*Plus, e.g. -V SQL*Plus version , -S silent  no banner , -M automatic HTML output etc.

Argument: ignore_ora_messages

This argument is used to ignore certain Oracle errors and expects a comma separated list of ORA-* error codes that should be ignored.

Argument: ignore_sp2_messages

This argument is used to ignore certain Oracle SP2 errors and expects a comma separated list of SP2* error codes that should be ignored.

Argument: include_files

Should some common SQL code be executed before the execution of the SQL*Plus script then the respective SQL script files can be specified by use of the include_files argument. A number of files can be specified by separating each file name with a semicolon.

Passing Arguments to SQL*Plus Scripts

Job arguments can be passed to the SQL*Plus script.

For example assume the following job arguments:

NameValue
BCY_DATE28.07.2014
PN_YEAR_PERIOD2014
PN_YEAR_PREV_PERIOD2013


The SQL*Plus script can include references to job arguments with the ${variable} syntax. Occurrences of variables are substituted before execution of the SQL*Plus script.

Passing variables to the SQL script
WHENEVER SQLERROR EXIT SQL.SQLCODE
WHENEVER OSERROR EXIT FAILURE
column end_date new_value BCY_DATE
column period new_value PN_YEAR_PERIOD
column period_prev new_value PN_YEAR_PREV_PERIOD
select '0' as end_date from dual;
prompt SET end_date IS ${BCY_DATE};
/
select '0' as period from dual;
prompt SET period IS ${PN_YEAR_PERIOD};
/
select '0' as period_prev from dual;
prompt SET period_prev IS ${PN_YEAR_PREV_PERIOD};
/
select sysdate from dual;
prompt SET end_date IS 1;
exit;

Passing SQL execution results to subsequent Jobs

The job allows use of the variable_parser_reg_expr argument. The argument's default value specifies a regular expression:

^SETs+(\\s)\\s*ISs(.*)$

This expression matches output that is created with the DBMS_OUTPUT package and expects the output to look like this:

SET my_variable IS some_value

As a result an Order Variable with the name my_variable and the value some_value is created. This pattern can be applied to any number of lines each creating a new order variable for subsequent instructions and jobs.

Return Variables created by the SQLPLUSJob

The job automatically returns the following Order Variables that are available to subsequent instructions and jobs. 

Return Variable: sql_error

The sql_error variable holds the error messages occurring during SQL*Plus execution. This variable is empty if no errors occur.

Return Variable: std_out_output

The std_out_output variable holds the messages spooled to the stdout channel by SQL*Plus.

Return Variable: std_err_output

The std_err_output variable contains the messages spooled to the stderr channel by SQL*Plus.

Return Variable: exit_code

  • The exit_code will be 0 If the SQL*Plus script terminates successfully.
  • If std_err_output is not empty then the exit code will be set to the value 99.
  • If either SP2* or ORA-* errors occur then the exit code will be set to the value 98.

Cancellation

The following operations are available:

  • The cancel operation is applied to the order, not to the SQLPlusJob. In case of cancellation the job will continue until completion and the order will be set to the failed state.
  • The cancel/force operation is applied to the order and to the job. The SQLPlusJob will be forcibly terminated and the shell process executing the sqlplus command line utility will be terminated. The database connection will be closed and the DBMS session will be terminated. The order will be set to the failed state. The DBMS decides about rolling back transactions when closing the client session.

Further Resources