BATSQL (Batch SQL) batch command

Purpose

To run an SQL statement (delete, insert, or update) that might be issued directly. The number of rows that might be changed by the statement is so large that lock escalation on the target Db2® table is an issue. To avoid lock escalation problems, a three-stage process is used to run the statement:

  1. An SQL select statement is generated. The result table for this statement includes all of the rows that are subject to delete, insert, or update.
  2. Rows from the result table are retrieved and the requested operation (delete, insert, or update) is applied to each row. Rows can be retrieved either one-at-a-time, or in a rowset (more than one row is retrieved at once).
  3. After the stipulated number of rows are processed, a Db2® commit is issued to release any locks currently held.

Usage

The FM/Db2 batch SQL utility can take these actions:

  • Delete rows from a specified table. Potentially all rows might be deleted if no WHERE clause is specified.
  • Update rows in a specified table. Potentially all rows might be updated if no WHERE clause is specified.
  • Insert rows into a specified table, by using the result table from a full select to generate the rows to be inserted.
  • Issue Db2® commits after a specified number of changes

The BATSQL is a batch-only function, there is no equivalent full-screen option. The BATSQL function runs a single SQL DELETE, INSERT, or UPDATE operation.

(1)
Notes:
  • 1 TYPE=DELETE, UPDATE
  • 2 TYPE=INSERT
  • 3 TYPE=DELETE
  • 4 TYPE=UPDATE

1! AUTOCOMMIT=100
1 AUTOCOMMIT=cnt
1! MAXERRORS=0
1 MAXERRORS=errcnt
1! ROWSET=YES
1 ROWSET=NO
1! ISOLATION=NONE
1 ISOLATION=
2.1 UNCOMMREAD
2.1 CS
2.1 RS
2.1 RS_SLOCK
2.1 RS_ULOCK
2.1 XLOCK
2.1 RR
2.1 RR_SLOCK
2.1 RR_ULOCK
2.1 RR_XLOCK
AUTOCOMMIT=cnt
The number of successful operations that are processed before an automatic Db2® commit is issued. A value of zero indicates that no Db2® commits are issued until all rows are processed. Valid values are 0 - 2147483647.

The Db2® cursor that is used to read rows from the result table is declared 'WITH HOLD', so the Db2® commit does not close the cursor. Any locks that are associated with change activities are removed by the Db2® commit.

ISOLATION=value
An optional isolation specification to be included when the cursor that accesses the result table is declared. The default is NONE.

See the description of 'isolation clause' in the SQL Reference manual, 'SELECT' statement for a description of the values that can be specified.

UNCOMMREAD (UR) can be specified for TYPE=DELETE, UPDATE but is ignored and converted to CS.

MAXERRORS=errcnt
The maximum number of errors that can occur before a Db2® rollback is issued and function processing is terminated. When a row is processed, any negative SQLCODE is considered to be an error, and the error count is incremented by 1. When a rowset is processed, any negative SQLCODE is considered to be an error, and the error count is incremented by the number of rows in the current rowset. Valid values are 0 - 2147483647.
DELETEALL=<YES|NO>
A specification for delete processing, indicating that the intention is to delete all rows from the table. For TYPE=DELETE processing without a where clause, you must specify DELETEALL=YES.

This option is a safety feature, since specifying a delete without a where clause attempts to delete all rows from the table.

OBJIN=location.owner.name
Specifies three items:
  • The optional name of the Db2® remote server (location) where the object is located.
  • The optional name of the owner of the object (owner).
  • The object name (name) to be processed.

When location is not specified, the current (local) Db2® server is used. When the owner is not specified, the object name is qualified with the current SQLID.

Use OBJIN when the fully qualified name fits on a single line in the JCL deck. The last usable column is column 71. When the fully qualified name does not fit on a single line in the JCL deck, use one or more of the OBJILOCN, OBJIOWNR, OBJINAME keywords to specify the object.

OBJILOCN=location
The optional name of the Db2® remote server (location) where the object is located. See Specifying a Db2 object name.
OBJIOWNR=owner
The optional name of the owner of the object (owner) to be processed. See Specifying a Db2 object name.
OBJINAME=name
The object name (name) for the object to be processed. See Specifying a Db2 object name.
Note: Specify OBJIN, OBJILOCN, OBJIOWNR, and OBJINAME for TYPE=DELETE and TYPE=UPDATE.
ROWSET=<YES|NO>
A specification that indicates whether FM/Db2 should use rowset processing when it fetches rows, and possibly when it deletes and updates rows.
YES
Rowset processing should be used. The number of rows in each rowset is the same as the value specified for AUTOCOMMIT. If the AUTOCOMMIT keyword is omitted, the rowset size is the default (100 rows). Valid rowset sizes are 2 - 10000. When the AUTOCOMMIT value is outside this range, single-row processing is automatically selected.
NO
Use single-row processing.
OBJOUT=location.owner.name
Specifies three items:
  • The optional name of the Db2® remote server (location) where the target object is located.
  • The optional name of the owner of the target object (owner)
  • The target object name (name) to be processed.

When location is not specified, the current (local) Db2® server is used. When the owner is not specified, the target object name is qualified with the current SQLID.

Use OBJOUT when the fully qualified name fits on a single line in the JCL deck. The last usable column is column 71. When the fully qualified name does not fit on a single line in the JCL deck, use one or more of the OBJILOCN, OBJIOWNR, OBJINAME keywords to specify the target object.

OBJOLOCN=location
The optional name of the Db2® remote server (location) where the target object is located. See Specifying a Db2 object name.
OBJOOWNR=owner
The optional name of the owner of the target object (owner) to be processed. See Specifying a Db2 object name.
OBJONAME=name
The object name (name) for the target object to be processed. See Specifying a Db2 object name.
Note: Specify OBJOUT, OBJOLOCN, OBJOOWNR, and OBJONAME for TYPE=INSERT.
SQLDDWHR=ddinwhr (TYPE=DELETE, UPDATE)
The optional DD name that refers to a valid SQL where clause.

If this keyword is omitted, the where clause is assumed to be specified with DD DDINWHR.

For TYPE=DELETE, you can specify a null where clause, however you must also include the keyword DELETEALL=YES to process the delete statement.

SQLDDSET=ddinset (TYPE=UPDATE)
The optional DD name that refers to a valid SQL set clause (for TYPE=UPDATE only).

If this keyword is omitted, the set clause is assumed to be specified with DD DDINSET.

A SET clause must be specified.

SQLDDINS=ddinins (TYPE=INSERT)
The optional DD name that refers to a valid column list appropriate for an SQL insert statement, for example:
INSERT INTO <object>  (column name1, column name2, ..)

If this keyword is omitted, the column list is assumed to be specified by using DD DDININS.

A list of column names is optional. If a list of column names is not specified, the result table from the select statement (specified by using SQLDDSEL) ought to have the same number of columns as the number of columns in the target table. Every column name in the list must be a column name of the target table.

SQLDDSEL=ddinsel (TYPE=INSERT)
The optional DD name that refers to a select statement that specifies the result table for the rows to be inserted into the target object.

If this keyword is omitted, the select statement is assumed to be specified with DD DDINSEL. A select statement that identifies the rows to be inserted, is required.

SQLRECSZ=recsz
The optional record size for all input data sets. The default is 72. When the SQL clauses are specified in-line in the JCL deck, do not specify a value greater than 72. Valid values are 40 - 32760.

Hints and tips

Rowset processing is automatically selected when an AUTOCOMMIT value of 2 - 10000 is specified.

For DELETE, INSERT and UPDATE processing, FM/Db2 fetches the rowset number of rows in a single fetch. For DELETE and UPDATE processing only, FM/Db2 attempts to delete or update rowset number of rows by using a single SQL statement.

For insert, each row is always inserted by using a single-row INSERT statement.

A large region size can be required when using rowset processing and a large rowset size. FM/Db2 needs sufficient storage to store the data for the specified rowset number of rows.

For delete and update processing that uses rowsets, it is possible that the delete or update operation for the rowset fails. When this failure occurs FM/Db2 tries the delete or update operation again, for every row of the rowset, using single-row processing. The error that is encountered for the rowset delete or update does not increment the error counter, however any error against a single-row delete or update does increment the error counter.

Examples

The examples refer to these table definitions:


CREATE TABLE RFM0681A."Names"
  ("Name_Identifier" INTEGER NOT NULL
  ,"Last_Name"       VARCHAR(16) NOT NULL
  ,"First_Name"      VARCHAR(16) NOT NULL
  ,"Second_Name"     VARCHAR(16) NOT NULL
  ,"Sex"             CHAR(1)     NOT NULL
  ,"Date_Of_Birth"   DATE        NOT NULL
  );

CREATE TABLE RFM0681B."Names"
  ("Name_Identifier" INTEGER NOT NULL
  ,"Date_Of_Birth"   DATE
  ,"Sex"             CHAR(1)
  ,"First_Name"      VARCHAR(16)
  ,"Second_Name"     VARCHAR(16)
  ,"Last_Name"       VARCHAR(16)
  );

Example 1

Delete all rows from the table that do not have a valid sex ('F' or 'M'). Issue a Db2® commit after every 50 successful delete operations. Stop if there are any errors.


//FMNDB2   EXEC PGM=FMNDB2,PARM=('SSID=DFXX,SQID=XXXXXXX')
//SYSPRINT DD SYSOUT=*
//FMNTSPRT DD SYSOUT=*
//DDINWHR  DD *
WHERE ("Sex" <> 'M'
  AND  "Sex" <> 'F')
/*
//SYSIN DD *
$$FILEM BATSQL TYPE=DELETE,
$$FILEM OBJIN=RFM0681A."Names",
$$FILEM AUTOCOMMIT=50,
$$FILEM MAXERRORS=1

Example 2

Update all rows from the table that have:

  • A surname of 'Smith'
  • A sex of 'm'

Change these rows to have:

  • A surname of 'SMITH'
  • A given name of 'JAMES'
  • No second name
  • A sex of 'M'

Issue a Db2® commit after every 100 successful update operations. Stop if there are more than 10 errors.


//FMNDB2   EXEC PGM=FMNDB2,PARM=('SSID=DFXX,SQID=XXXXXXX')
//SYSPRINT DD SYSOUT=*
//FMNTSPRT DD SYSOUT=*
//DDINSET  DD *
SET ("Last_Name", "First_Name", "Second_Name", "Sex") =
    ('SMITH',     'JAMES',      '',            'M')
/*
//DDINWHR  DD *
WHERE "Last_Name" = 'Smith'
  AND "Sex" = 'm'
/*
//SYSIN DD *
$$FILEM BATSQL TYPE=UPDATE,
$$FILEM OBJIN=RFM0681A."Names",
$$FILEM AUTOCOMMIT=100,
$$FILEM MAXERRORS=10
/*

Example 3

Insert rows into RFM0681B."Names", using a select against RFM0681A."Names" to produce a result table.

Only insert rows when the "Name_Identifier" value is less than or equal to 100,000.


//FMNDB2   EXEC PGM=FMNDB2,PARM=('SSID=DFXX,SQID=XXXXXXX')
//SYSPRINT DD SYSOUT=*
//FMNTSPRT DD SYSOUT=*
//DDININS  DD *
(
 "First_Name",
 "Second_Name",
 "Last_Name",
 "Sex",
 "Date_Of_Birth",
 "Name_Identifier"
)
/*
//DDINSEL DD *
SELECT
 "First_Name",
 "Second_Name",
 "Last_Name",
 "Sex",
 "Date_Of_Birth",
 "Name_Identifier"
FROM RFM0681A."Names"
WHERE "Name_Identifier" <= 100000
/*
//SYSIN DD *
$$FILEM BATSQL TYPE=INSERT,
$$FILEM OBJOUT=RFM0681B."Names",
$$FILEM AUTOCOMMIT=50,
$$FILEM MAXERRORS=100
/*