Introduction
The SOSSQLPlusJob JITL job provides a standardized and parameterized interface for executing Oracle SQL*Plus scripts. The JobScheduler offers out of the box capability to execute SQL scripts, passing parameters to the SQL*Plus script or collecting and passing on the results of a script execution to next job step as a JobScheduler Order parameter. The SOSSQLPlusJob can be used to execute existing SQL*Plus scripts by referring them in the command parameter.
A Simple JITL SQL*Plus Job Example
The following example shows a basic example of the SOSSQLPlusJob . It executes a simple SQL Statment - selecting the current system date and displaying it on stdout as order_date.
Parameters
The SOSSQLPlusJob requires the following parameters:
# | Name | Title | Mandatory | Default | Example |
---|---|---|---|---|---|
1 | command_script_file | SQL script to be executed | true |
|
|
2 | db_url | Database service or Instance name | true |
| DORCL01 |
3 | db_user | User name for database access | true |
| db username |
4 | db_password | Password for database access | true |
| db password |
5 | shell_command | SQL plus client name | true | sqlplus | |
6 | variable_parser_reg_expr | Regular expression to parse output and set order parameters for next job steps | false | ^SETs+(\\s)\\s*ISs(.*)$ |
|
7 | command_Line_options | SQL*Plus command line options | false | -S -L | |
8 | ignore_ora_messages | Ignore ora error messages | false | ||
9 | ignore_sp2_messages | ignore sp2 error messages | false | ||
10 | include_files | Code blocks should be executed before SQL | false |
command_script_file
- The SQL script can be:
saved to a separate file such as get_order.sql . This file can subsequently be referred to as the value of the "command_script_file" job parameter. This is a recommended approach for achieving "separation of concern" in application architecture.
<param name="command_script_file" value="config/live/commn/sqls/get_order.sql"/>
SQL code can also be specified as the value of the CDATA tag as part of the Job.XML. This approach is preferred only if the SQL code is very small and only used by a single job.
<script language="java" java_class="sos.scheduler.db.SOSSQLPlusJobJSAdapterClass"> <![CDATA[ WHENEVER SQLERROR EXIT SQL.SQLCODE WHENEVER OSERROR EXIT FAILURE SELECT TO_CHAR(SYSDATE,'dd.mm.yyyy DAY') FROM DUAL; ]]> </script>
db_url
For SQLPlus job db_url is only the Oracle DB service name or instance name
db_user
DB Username which has necessary database permission to execute SQL script.
db_password
The password for the DB user defined in the db_user parameter.
shell_command
The value of this parameter specifies the name of SQL*Plus client. The SQL sctipt will be processed by the client. This parameter is useful when executing legacy SQL code i.e. Oracle 9i, 10g
variable_parser_reg_expr
By default the SOSSQLPlusJob job defines a regular expression to parse console output from the execution of SQL Script and sets order parameters for subsequent job steps. For example, the SQL*Plus client stdout prompt SET period_prev IS ${PN_YEAR_PREV_PERIOD} displays the output on console; if period_prev is 20140915, it will be parsed by regular expression ^SETs+(\\s)\\s*ISs(.*)$ and return the period_prev="20140915" order parameter. All stdout statements matching the ^SETs+(\\s)\\s*ISs(.*)$ regular expression will be set as order_parameters.
command_Line_options
The value of this parameter specifies all the command line parameters required by the SQL*Plus client i.e. -V SQL*Plus version , -S silent no banner , -M automatic HTML output etc.
ignore_ora_messages
Its not always the case when you want to ignore error messages but if need be arise, value of the parameter ignore_ora_message will specify which ORA messages Jobscheduler can ignore.
ignore_sp2_messages
If there are some expected Oracle SP2 errors which you want to ignore, use a comma separated list of all the SP2 errors as value for the ignore_sp2_messages parameter.
include_files
If you have some common code need to be executed before SQL*Plus code, you can define with include_files parameter. Multiple files can be specified, seperated by semicolons.
Saving Database Connection Settings in a Parameter File
It strongly recommend that a db_connection parameter file such as database_connection_sqlplus.parameter.xml is used to store all the database connection settings in a common location. This approach enables the user to manage settings at central location which can then be reused by multiple jobs.
This approach also makes it easy to maintain different settings for development, integration and production environments.
The following shows an example database connection parameter file:
The next example shows a JITL job where the database connection parameters are stored in an external file. In this example a "common_settings/database" directory has been created inside the JobScheduler's live folder.
Passing parameters to the SQL Script
JobScheduler order parameters can be passed to the SQL Script. SQL Script code can be parameterized by defining variables such as ${SCHEDULER_PARAM_VARIABLE_NAME}. Variables can be set using environment variables, JobScheduler task parameters ( as described in the following example) or from JobScheduler order parameters.
Parameters can also be defined with following syntax:
- %parameter_name%
- ${SCHEDULER_PARAM_parameter_name}
Parameters are not case sensitive.
SQL Script as an External File
SQL script can be defined directly inside the Job xml as a command parameter value but is generally better stored on the file system. JITL jobs can be configured to read PL/SQL scripts from the file system by defining the script path as a value for the command parameter i.e.
In the following example the PL/SQL code is saved to the filesystem in C:\app\executables\sqls\get_booking_cycle.sql and subsequently referenced using the command_script_file parameter.
Passing SQL script results to subsequent job steps as parameters
JobScheduler jobs can create and update JobScheduler Order parameters. The SOSSQLPlusJob can also pass on the result of SQL script execution i.e. calculated dates, parameters calculated from tables, etc. By default the JobSchedulerSQLPlus job defines a regular expression to parse console output from the execution of SQL Script and sets order parameters for subsequent job steps. For example, the SQL*Plus client stdout prompt SET period_prev IS ${PN_YEAR_PREV_PERIOD} displays the output on console; if period_prev is 20140915, it will be parsed by regular expression ^SETs+(\\s)\\s*ISs(.*)$ and return the period_prev="20140915" order parameter. All stdout statements matching the ^SETs+(\\s)\\s*ISs(.*)$ regular expression will be set as order_parameters.
Advanced Configuration
Generic job for executing multiple SQL scripts
The SOSSQLPlusJob can be configured as a generic node inside a job chain and executable SQL script can be defined as an order parameter. The following example shows such a generic job. The job chain has a job node - execute_sql - two orders - get_booking_cycle and count_trx_records . Each order is scheduled to be executed at a different time. Both orders are configured to use a different SQL script file i.e. get_booking_cycle.sql and count_trx_records.sql.
JobChain
Job
Order : get_booking_cycle
Order : get_trx_count
Return parameters created by the SOSSQLPlusJob
The JobScheduler automatically creates the following order parameters, which will then be available to subsequent job steps.
sql_error
- The sql_error parameter contains all the error messages generated during the SQL script execution. This parameter will be empty if errors do not occur.
std_out_output
- The std_out_output parameter contains all the messages spooled to stdout by SQL Script.
std_err_output
- The std_out_output parameter contains all the messages spooled to stdout by SQL Script.
exit_code
- The exit_code will be 0 If script ended without any error
- If std_error_output is not empty the exit code will be set to 99
- If either SP2 or an ORA- error occurs the exit code will be set to 98
See also:
- How to work with PL/SQL and the Oracle DBMS
- How to run Oracle Stored Procedures using PL/SQL
- The SOSSQLPlusJob JITL job documentation