DB2
DB2
DB2
Contents v
Recovering data in a database file . . . . . . 215 Primary key constraints . . . . . . . . . . 242
Managing journals . . . . . . . . . . 215 Check constraints . . . . . . . . . . . . 242
Journals . . . . . . . . . . . . . . 216
Ensuring data integrity with commitment Chapter 17. Ensuring data integrity
control . . . . . . . . . . . . . . 216 with referential constraints . . . . . 245
Reducing time in access path recovery . . . . . 218
Adding a referential constraint . . . . . . . 245
Saving access paths . . . . . . . . . . 218
Before you add a referential constraint . . . . 246
Restoring access paths . . . . . . . . . 219
Defining the parent file in a referential
Journaling access paths . . . . . . . . . 219
constraint. . . . . . . . . . . . . . 246
System-managed access-path protection
Defining the dependent file in a referential
(SMAPP) . . . . . . . . . . . . . . 220
constraint. . . . . . . . . . . . . . 247
Rebuilding access paths . . . . . . . . . 220
Specifying referential constraint rules . . . . 247
The database recovery process after an abnormal
Details: Specifying referential constraint delete
system end . . . . . . . . . . . . . . 223
rules . . . . . . . . . . . . . . . 248
Database file recovery during the IPL . . . . 223
Details: Specifying referential constraint update
Database file recovery after the IPL . . . . . 224
rules . . . . . . . . . . . . . . . 248
Effects of the storage pool paging option on
Details: Specifying referential constraint
database recovery . . . . . . . . . . . 224
rules—journaling requirements . . . . . . 249
Database file recovery options table . . . . . 225
Details: Adding a referential constraint . . . . 249
Database save and restore . . . . . . . . . 225
Details: Avoiding constraint cycles . . . . . 250
Database considerations for save and restore . . . 226
Verifying a referential constraint . . . . . . . 250
Force-writing data to auxiliary storage . . . . 226
Enabling and disabling referential constraints . . 250
Details: Enabling or disabling a referential
Chapter 15. Using Source Files. . . . 227 constraint. . . . . . . . . . . . . . 250
Source File Concepts . . . . . . . . . . . 227 Removing referential constraints . . . . . . . 251
Creating a Source File . . . . . . . . . . 227 Details: Removing a constraint with the CST
IBM-Supplied Source Files . . . . . . . . 228 parameter . . . . . . . . . . . . . 252
Source File Attributes. . . . . . . . . . 228 Details: Removing a constraint with the TYPE
Creating Source Files without DDS . . . . . 229 parameter . . . . . . . . . . . . . 252
Creating Source Files with DDS . . . . . . 229 Details: Ensuring data integrity with referential
Working with Source Files . . . . . . . . . 230 constraints . . . . . . . . . . . . . . 252
Using the Source Entry Utility (SEU) . . . . 230 Example: Ensuring data integrity with referential
Using Device Source Files . . . . . . . . 230 constraints . . . . . . . . . . . . . . 253
Copying Source File Data . . . . . . . . 230 Referential integrity terms . . . . . . . . . 253
Loading and Unloading Data from Non-AS/400 Referential integrity enforcement . . . . . . . 254
Systems . . . . . . . . . . . . . . 232 Foreign key enforcement . . . . . . . . 254
Using Source Files in a Program . . . . . . 232 Parent key enforcement . . . . . . . . . 254
Creating an Object Using a Source File . . . . . 233 Constraint states . . . . . . . . . . . . 255
Creating an Object from Source Statements in a Check pending status in referential constraints . . 256
Batch Job . . . . . . . . . . . . . . 233 Dependent file restrictions in check pending . . 256
Determining Which Source File Member Was Parent file restrictions in check pending . . . 257
Used to Create an Object . . . . . . . . 234 Referential integrity and AS/400 functions . . . 257
Managing a Source File . . . . . . . . . . 235
Changing Source File Attributes . . . . . . 235 Chapter 18. Triggering automatic
Reorganizing Source File Member Data. . . . 235
Determining When a Source Statement Was
events in your database . . . . . . . 261
Changed . . . . . . . . . . . . . . 236 Creating trigger programs . . . . . . . . . 262
Using Source Files for Documentation . . . . 236 Examples of trigger programs . . . . . . . 262
Trigger buffer section . . . . . . . . . . 276
Recommendations for trigger programs . . . 278
Chapter 16. Controlling the integrity of Precautions to take when coding trigger
your database with constraints. . . . 237 programs . . . . . . . . . . . . . . 278
Setting up constraints for your database . . . . 237 Trigger and application programs that are under
Details: Setting up constraints . . . . . . . 237 commitment control . . . . . . . . . . 280
Removing constraints from your database . . . . 238 Trigger and application programs that are not
Details: Removing constraints . . . . . . . 238 under commitment control . . . . . . . . 281
Working with a group of constraints . . . . . 239 Trigger program error messages . . . . . . 281
Details: Working with a group of constraints 239 | Monitoring the Use of Trigger Programs . . . 281
Working with constraints that are in check Adding a Trigger to a File . . . . . . . . . 282
pending status . . . . . . . . . . . . 240 Required authorities and data capabilities for
Unique constraints . . . . . . . . . . . 242 triggers . . . . . . . . . . . . . . 283
Contents vii
viii DB2 UDB for AS/400 Database Programming V4R5
About DB2 UDB for AS/400 Database Programming
This book contains information about the DB2 UDB for AS/400 database
management system, and describes how to set up and use a database on AS/400.
This book does not cover in detail all of the capabilities on AS/400 that are related
to database. Among the topics not fully described are the relationships of the
following topics to database management:
v Structured Query Language (SQL)
v Data description specifications (DDS)
v Control language (CL)
v Interactive data definition utility (IDDU)
v Backup and recovery guidelines and utilities
v User-defined functions (UDFs), large objects (LOBs), user-defined types (UDTs),
and DataLinks. See DB2 UDB for AS/400 SQL Programming Concepts for more
information.
Who should read the DB2 UDB for AS/400 Database Programming
book
This book is intended for the system administrator or programmer who creates
and manages files and databases on AS/400. In addition, this book is intended for
programmers who use the database in their programs.
Before using this book, you should be familiar with the introductory material for
using the system. You should also understand how to write a high-level language
program for AS/400. Use this book with the high-level language books to get
| additional database information, tips, and techniques.
|
| What’s new for V4R5 in the DB2 UDB for AS/400 Database
| Programming book
| The following topics were updated in this release of the information:
| v “Performing Tasks in Operations Navigator Database” on page 93
| v “Chapter 4. Securing a Database” on page 87
| v “Locking shared data” on page 107
| v “Reorganizing a Table” on page 197
| v “Ensuring data integrity with commitment control” on page 216
| v “Monitoring the Use of Trigger Programs” on page 281
This part includes a chapter with guidelines for describing and creating logical
files. This includes information on describing logical file record formats and
different types of field use using data description specifications (DDS). Also
information is included on describing access paths using DDS as well as using
access paths that already exist in the system. Information on defining logical file
members to separate the data into logical groups is also included in this chapter.
A section on join logical files includes considerations for using join logical files,
including examples on how to join physical files and the different ways physical
files can be joined. Information on performance, integrity, and a summary of rules
for join logical files is also included.
Regardless of whether a file is described to the field or record level, you must
describe and create the file before you can compile a program that uses that file.
That is, the file must exist on the system before you use it.
1 The program uses the field level description of a file that is defined to the
system. At compilation time, the language compiler copies the external
description of the file into the program.
2 The program uses a file that is described to the field level to the system,
but it does not use the actual field descriptions. At compilation time, the
language compiler does not copy the external description of the file into
the program. The fields in the file are described in the program. In this
case, the field attributes (for example, field length) used in the program
must be the same as the field attributes in the external description.
3 The program uses a file that is described only to the record level to the
system. The fields in the file must be described in the program.
Externally described files can also be described in a program. You might want to
use this method for compatibility with previous systems. For example, you want to
run programs on the AS/400 system that originally came from a traditional file
system. Those programs use program-described data, and the file itself is only
described to the record level. At a later time, you describe the file to the field level
(externally described file) to use more of the database functions available on the
system. Your old programs, containing program-described data, can continue to
use the externally described file while new programs use the field-level
descriptions that are part of the file. Over time, you can change one or more of
your old programs to use the field level descriptions.
Dictionary-described data
A program-described file can be dictionary-described. You can describe the record
format information using interactive data definition utility (IDDU). Even though
the file is program-described, AS/400 Query, Client Access, and data file utility
(DFU) will use the record format description stored in the data dictionary.
An externally described file can also be dictionary-described. You can use IDDU to
describe a file, then create the file using IDDU. The file created is an externally
described file. You can also move into the data dictionary the file description
stored in an externally described file. The system always ensures that the
definitions in the data dictionary and the description stored in the externally
described file are identical.
If you want to describe your file to the field level, several methods can be used to
describe data to the database system: IDDU, SQL* commands, or data description
specifications (DDS).
Note: Because DDS has the most options for defining data for the programmer,
this guide will focus on describing database files using DDS.
| When you use IDDU to describe your files, the file definition becomes part of the
| OS/400 data dictionary.
For more information about IDDU, see the IDDU Use book.
SQL was created by IBM to meet the need for a standard and common database
language. It is currently used on all IBM DB2 platforms and on many other
database implementations from many different manufacturers.
When database files are created using the DB2 UDB for AS/400 SQL language, the
description of the file is automatically added to a data dictionary in the SQL
collection. The data dictionary (or catalog) is then automatically maintained by the
system.
The SQL language is the language of choice for accessing databases on many other
platforms. It is the only language for distributed database and heterogeneous
systems.
For more information about SQL, see DB2 UDB for AS/400 SQL Programming
Concepts and DB2 UDB for AS/400 SQL Reference.
The DDS Form provides a common format for describing data externally. DDS data
is column sensitive. The examples in this manual have numbered columns and
show the data in the correct columns.
DB2 UDB for AS/400 SQL Reference contains a detailed description of DDS
functions to describe physical and logical files.
The following example shows the relationship between the record format and the
records in a physical file:
A physical file can have only one record format. The record format in a physical
file describes the way the data is actually stored.
A logical file contains no data. Logical files are used to arrange data from one or
more physical files into different formats and sequences. For example, a logical file
could change the order of the fields in the physical file, or present to the program
only some of the fields stored in the physical file.
A logical file record format can change the length and data type of fields stored in
physical files. The system does the necessary conversion between the physical file
field description and the logical file field description. For example, a physical file
could describe FLDA as a packed decimal field of 5 digits and a logical file using
FLDA might redefine it as a zoned decimal field of 7 digits. In this case, when
your program used the logical file to read a record, the system would
automatically convert (unpack) FLDA to zoned decimal format.
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A* ORDER HEADER FILE (ORDHDRP)
A 5
A 1 UNIQUE
A 2 R ORDHDR TEXT('Order header record')
A 3 CUST 5 0 TEXT('Customer number')
A ORDER 5 0 TEXT('Order number')
A .
A .
A .
A K CUST
A 4 K ORDER
The following example shows a physical file ORDHDRP (an order header file),
which has an arrival sequence access path without key fields specified, and the
DDS necessary to describe that file.
| The R in position 17 indicates that a record format is being defined. The record
| format name ORDHDR is specified in positions 19 through 28.
| You make no entry in position 17 when you are describing a field; a blank in
| position 17 along with a name in positions 19 through 28 indicates a field name.
The data type is specified in position 35. The valid data types are:
Entry Meaning
A Character
P Packed decimal
S Zoned decimal
B Binary
F Floating point
H Hexadecimal
L Date
T Time
Z Timestamp
Notes:
1. For double-byte character set (DBCS) data types, see Appendix B. Double-Byte
Character Set (DBCS) Considerations.
2. The AS/400 system performs arithmetic operations more efficiently for packed
decimal than for zoned decimal.
3. Some high-level languages do not support floating-point data.
4. Some special considerations that apply when you are using floating-point fields
are:
v The precision associated with a floating-point field is a function of the
number of bits (single or double precision) and the internal representation of
the floating-point value. This translates into the number of decimal digits
supported in the significant and the maximum values that can be represented
in the floating-point field.
If the data type (position 35) is not specified, the decimal positions entry is used to
determine the data type. If the decimal positions (positions 36 through 37) are
blank, the data type is assumed to be character (A); if these positions contain a
number 0 through 31, the data type is assumed to be packed decimal (P).
The length of the field is specified in positions 30 through 34, and the number of
decimal positions (for numeric fields) is specified in positions 36 and 37. If a
packed or zoned decimal field is to be used in a high-level language program, the
field length must be limited to the length allowed by the high-level language you
are using. The length is not the length of the field in storage but the number of
digits or characters specified externally from storage. For example, a 5-digit packed
decimal field has a length of 5 specified in DDS, but it uses only 3 bytes of storage.
You can use the DDS VARLEN keyword to define a character field as variable
length. You can define this field as:
v Variable-length with no allocated length. This allows the field to be stored using
only the number of bytes equal to the data (plus two bytes per field for the
length value and a few overhead bytes per record). However, performance might
be affected because all data is stored in the variable portion of the file, which
requires two disk read operations to retrieve.
v Variable-length with an allocated length equal to the most likely size of the data.
This allows most field data to be stored in the fixed portion of the file and
minimizes unused storage allocations common with fixed-length field
definitions. Only one read operation is required to retrieve field data with a
length less than the allocated field length. Field data with a length greater than
the allocated length is stored in the variable portion of the file and requires two
read operations to retrieve the data.
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A* ORDER HEADER FILE (ORDHDRP)
A 6
A 1 UNIQUE
A 2 R ORDHDR PFILE(ORDHDRP)
A 3 ORDER TEXT('Order number')
A CUST TEXT('Customer number')
A .
A .
A .
A 4 K ORDER
A O OPNSTS 5 CMP(EQ 'N')
A S ALL
A logical file must be created after all physical files on which it is based are
created. The PFILE keyword in the previous example is used to specify the
physical file or files on which the logical file is based.
Fields in the logical file record format must either appear in the record format of at
least one of the physical files or be derived from the fields of the physical files on
which the logical file is based.
For more information about describing logical files, see Chapter 3. Setting Up
Logical Files.
In addition, you can create a physical file for the sole purpose of using its field
descriptions. That is, the file does not contain data; it is used only as a reference
for the field descriptions for other files. This type of file is known as a field
reference file. A field reference file is a physical file containing no data, just field
descriptions.
You can use a field reference file to simplify record format descriptions and to
ensure field descriptions are used consistently. You can define all the fields you
need for an application or any group of files in a field reference file. You can create
a field reference file using DDS and the Create Physical File (CRTPF) command.
After the field reference file is created, you can build physical file record formats
from this file without describing the characteristics of each field in each file. When
you build physical files, all you need to do is refer to the field reference file (using
the REF and REFFLD keywords) and specify any changes. Any changes to the field
descriptions and keywords specified in your new file override the descriptions in
the field reference file.
In the following example, a field reference file named DSTREFP is created for
distribution applications. Figure 3 on page 13 shows the DDS needed to describe
DSTREFP.
Assume that the DDS in Figure 3 is entered into a source file FRSOURCE; the
member name is DSTREFP. To then create a field reference file, use the Create
Physical File (CRTPF) command as follows:
CRTPF FILE(DSTPRODLB/DSTREFP)
SRCFILE(QGPL/FRSOURCE) MBR(*NONE)
TEXT('Distribution field reference file')
The parameter MBR(*NONE) tells the system not to add a member to the file
(because the field reference file never contains data and therefore does not need a
member).
To describe the physical file ORDHDRP by referring to DSTREFP, use the following
DDS ( Figure 4 on page 15):
Figure 4. DDS for a Physical File (ORDHDRP) Built from a Field Reference File
The REF keyword (positions 45 through 80) with DSTREFP (the field reference file
name) specified indicates the file from which field descriptions are to be used. The
R in position 29 of each field indicates that the field description is to be taken from
the reference file.
When you create the ORDHDRP file, the system uses the DSTREFP file to
determine the attributes of the fields included in the ORDHDR record format. To
create the ORDHDRP file, use the Create Physical File (CRTPF) command. Assume
that the DDS in Figure 4 was entered into a source file QDDSSRC; the member
name is ORDHDRP.
CRTPF FILE(DSTPRODLB/ORDHDRP)
TEXT('Order Header physical file')
Note: The files used in some of the examples in this guide refer to this field
reference file.
The file originally describing the record format can be deleted without affecting the
files sharing the record format. After the last file using the record format is deleted,
the system automatically deletes the record format description.
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A R RECORD1 PFILE(CUSMSTP)
A CUST
A NAME
A ADDR
A SEARCH
A K CUST
A
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A R RECORD1 PFILE(CUSMSTP)
A FORMAT(CUSMSTL)
A K NAME
A
The example shown in Figure 5 shows file CUSMSTL, in which the fields Cust,
Name, Addr, and Search make up the record format. The Cust field is specified as a
key field.
The DDS in Figure 6 shows file CUSTMSTL1, in which the FORMAT keyword
names CUSMSTL to supply the record format. The record format name must be
RECORD1, the same as the record format name shown in Figure 5. Because the
files are sharing the same format, both files have fields Cust, Name, Addr, and
Search in the record format. In file CUSMSTL1, a different key field, Name is
specified.
If the original record format is changed by deleting all related files and creating the
original file and all the related files again, it is changed for all files that share it. If
only the file with the original format is deleted and re-created with a new record
format, all files previously sharing that file’s format continue to use the original
format.
If a logical file is defined but no field descriptions are specified and the FORMAT
keyword is not specified, the record format of the first physical file (specified first
on the PFILE keyword for the logical file) is automatically shared. The record
format name specified in the logical file must be the same as the record format
name specified in the physical file.
To find out if a file shares a format with another file, use the RCDFMT parameter
on the Display Database Relations (DSPDBR) command.
Record format sharing limitation with physical and logical database files: A
record format can only be shared by 32K objects. Error messages are issued when
you reach the limitation. You may encounter this limitation in a circumstance
where you are duplicating the same database object multiple times.
Note: Format sharing is performed for files that are duplicated. The format is
shared up to 32,767 times. Beyond that, if a file that shares the format is
duplicated, a new format will be created for the duplicated file.
An externally described file has an arrival sequence access path when no key fields
are specified for the file.
Key fields defined as character fields are arranged based on the sequence defined
for EBCDIC characters. Key fields defined as numeric fields are arranged based on
their algebraic values, unless the UNSIGNED (unsigned value) or ABSVAL
(absolute value) DDS keywords are specified for the field. Key fields defined as
DBCS are allowed, but are arranged only as single bytes based on their bit
representation.
If the Empname is the key field and is a character field, using the sequence for
EBCDIC characters, the records would be arranged as follows:
Notice that the EBCDIC sequence causes an unexpected sort order because the
lowercase characters are sorted before uppercase characters. Thus, Smith, Ron sorts
To use an alternative collating sequence for a character key field, specify the
ALTSEQ DDS keyword, and specify the name of the table containing the
alternative collating sequence. When setting up a table, each 2-byte position in the
table corresponds to a character. To change the order in which a character is sorted,
change its 2-digit value to the same value as the character it should be sorted equal
to. For more information about the ALTSEQ keyword, see DDS Reference. For
information about sorting uppercase and lowercase characters regardless of their
case, the QCASE256 table in library QUSRSYS is provided for you.
Arranging Key Fields Using the SRTSEQ Parameter: You can arrange key fields
containing character data according to several sorting sequences available with the
SRTSEQ parameter. Consider the following records:
If the Empname field is the key field and is a character field, the *HEX sequence
(the EBCDIC sequence) arranges the records as follows:
Notice that with the *HEX sequence, all lowercase characters are sorted before the
uppercase characters. Thus, Smith, Ron sorts before Smith, ROBERT, and JOHNSON,
JOHN sorts between the lowercase and uppercase Jones. You can use the
*LANGIDSHR sort sequence to sort records when the records were entered using a
mixture of uppercase and lowercase. The *LANGIDSHR sequence, which uses the
same collating weight for lowercase and uppercase characters, results in the
following:
Notice that with the *LANGIDSHR sequence, the lowercase and uppercase
characters are treated as equal. Thus, JONES, MARTIN and Jones, Martin are equal
and sort in the same sequence they have in the base file. While this is not incorrect,
it would look better in a report if all the lowercase Jones preceded the uppercase
JONES. You can use the *LANGIDUNQ sort sequence to sort the records when the
records were entered using an inconsistent uppercase and lowercase. The
*LANGIDUNQ sequence, which uses different but sequential collating weights for
lowercase and uppercase characters, results in the following:
The *LANGIDSHR and *LANGIDUNQ sort sequences exist for every language
supported in your system. The LANGID parameter determines which
*LANGIDSHR or *LANGIDUNQ sort sequence to use. Use the SRTSEQ parameter
to specify the sort sequence and the LANGID parameter to specify the language.
If the Empnbr field is the key field, the two possibilities for organizing these
records are:
v In ascending sequence, where the order of the records in the access path is:
When you describe a key field, the default is ascending sequence. However, you
can use the DESCEND DDS keyword to specify that you want to arrange a key
field in descending sequence.
Using More Than One Key Field: You can use more than one key field to
arrange the records in a file. The key fields do not have to use the same sequence.
For example, when you use two key fields, one field can use ascending sequence
while the other can use descending sequence. Consider the following records:
If the access path uses the Order field, then the Line field as the key fields, both in
ascending sequence, the order of the records in the access path is:
If the access path uses the key field Order in ascending sequence, then the Line
field in descending sequence, the order of the records in the access path is:
When a record has key fields whose contents are the same as the key field in
another record in the same file, then the file is said to have records with duplicate
key values. However, the duplication must occur for all key fields for a record if
they are to be called duplicate key values. For example, if a record format has two
key fields Order and Ordate, duplicate key values occur when the contents of both
the Order and Ordate fields are the same in two or more records. These records
have duplicate key values:
Using the Line field as a third key field defines the file so that there are no
duplicate keys:
A logical file that has more than one record format can have records with duplicate
key values, even though the record formats are based on different physical files.
That is, even though the key values come from different record formats, they are
considered duplicate key values.
You can prevent duplicate key values in your files by specifying the UNIQUE
keyword in DDS. With the UNIQUE keyword specified, a record cannot be entered
or copied into a file if its key value is the same as the key value of a record
already existing in the file. You can also use unique constraints to enforce the
integrity of unique keys. For details on the supported constraints, see “Chapter 16.
Controlling the integrity of your database with constraints” on page 237.
If records with duplicate key values already exist in a physical file, the associated
logical file cannot have the UNIQUE keyword specified. If you try to create a
logical file with the UNIQUE keyword specified, and the associated physical file
contains duplicate key values, the logical file is not created. The system sends you
a message stating this and sends you messages (as many as 20) indicating which
records contain duplicate key values.
When the UNIQUE keyword is specified for a file, any record added to the file
cannot have a key value that duplicates the key value of an existing record in the
file, regardless of the file used to add the new record. For example, two logical
files LF1 and LF2 are based on the physical file PF1. The UNIQUE keyword is
specified for LF1. If you use LF2 to add a record to PF1, you cannot add the record
if it causes a duplicate key value in LF1.
If any of the key fields allow null values, null values that are inserted into those
fields may or may not cause duplicates depending on how the access path was
defined at the time the file was created. The *INCNULL parameter of the UNIQUE
keyword indicates that null values are included when determining whether
duplicates exist in the unique access path. The *EXCNULL parameter indicates that
null values are not included when determining whether duplicate values exist. For
more information, see DDS Reference.
The following shows the DDS for a logical file that requires unique key values:
In this example, the contents of the key fields (the Order field for the ORDHDR
record format, and the Order and Line fields for the ORDDTL record format) must
be unique whether the record is added through the ORDHDRP file, the ORDDTLP
file, or the logical file defined here. With the Line field specified as a second key
field in the ORDDTL record format, the same value can exist in the Order key field
in both physical files. Because the physical file ORDDTLP has two key fields and
the physical file ORDHDRP has only one, the key values in the two files do not
conflict.
Arranging Duplicate Keys: If you do not specify the UNIQUE keyword in DDS,
you can specify how the system is to store records with duplicate key values,
should they occur. You specify that records with duplicate key values are stored in
the access path in one of the following ways:
v Last-in-first-out (LIFO). When the LIFO keyword is specified (1), records with
duplicate key values are retrieved in last-in-first-out order by the physical
sequence of the records. Below is an example of DDS using the LIFO keyword.
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A* ORDERP2
A 1 LIFO
A R ORDER2
A .
A .
A .
A K ORDER
A
When a simple- or multiple-format logical file is based on more than one physical
file member, records with duplicate key values are read in the order in which the
files and members are specified on the DTAMBRS parameter on the Create Logical
File (CRTLF) or Add Logical File Member (ADDLFM) command. Examples of
logical files with more than one record format can be found in the DDS Reference.
Records 3 and 4, which have duplicate key values, are in FIFO order. That is,
because record 3 was added to the file before record 4, it is read before record 4.
This would become apparent if the records were read in descending order. This
could be done by creating a logical file based on this physical file, with the
DESCEND keyword specified in the logical file.
If physical record 1 is changed such that the key value is C, the sequence of the
access path for the physical file is (FIFO, ascending key):
Finally, changing to descending order, the new sequence of the access path for the
logical file is (FIFO, descending key):
After the change, record 1 does not appear after record 4, even though the contents
of the key field were updated after record 4 was added.
The FCFO order of records with duplicate key values is determined by the
sequence of updates made to the contents of the key fields. In the example above,
after record 1 is changed such that the key value is C, the sequence of the access
path (FCFO, ascending key only) is:
For FCFO, the duplicate key ordering can change when the FCFO access path is
rebuilt or when a rollback operation is performed. In some cases, your key field
can change but the physical key does not change. In these cases, the FCFO
ordering does not change, even though the key field has changed. For example,
when the index ordering is changed to be based on the absolute value of the key,
the FCFO ordering does not change. The physical value of the key does not change
even though your key changes from negative to positive. Because the physical key
does not change, FCFO ordering does not change.
If the reuse deleted records attribute is specified for a physical file, the duplicate
key ordering must be allowed to default or must be FCFO. The reuse deleted
records attribute is not allowed for the physical file if either the key ordering for
the file is FIFO or LIFO, or if any of the logical files defined over the physical file
have duplicate key ordering of FIFO or LIFO.
A floating-point key field with the SIGNED keyword specified, or defaulted to, on
the DDS has an algebraic numeric sequence. The collating sequence is negative
infinity—real numbers—positive infinity.
A floating-point key field with the ABSVAL keyword specified on the DDS has an
absolute value numeric sequence.
You cannot use not-a-number (*NAN) values in key fields. If you attempt this, and
a *NAN value is detected in a key field during file creation, the file is not created.
You can create a database file by using Interactive Data Definition Utility (IDDU).
If you are using IDDU to describe your database files, you might also consider
using it to create your files.
You can create a database file by using Structured Query Language (SQL)
statements. SQL is the IBM relational database language, and can be used on
AS/400 to interactively describe and create database files.
You can also create a database file by using CL. The CL database file create
commands are: Create Physical File (CRTPF), Create Logical File (CRTLF), and
Create Source Physical File (CRTSRCPF).
Copying a file
| You can copy a file using the Copy a Table operation in Operations Navigator. Or,
| you can use the Copy File (CPYF) command.
Moving an object
| You can move an object using the Move a Table operation in Operations Navigator.
| Or, you can use the Move Object (MOVOBJ) command.
Note: The system does not automatically create a member for a source physical
file.
For more information about using logical files in this way, see “Logical File
Members” on page 58.
The system finds a place for the file on auxiliary storage. To specify where to store
the file, use the UNIT parameter. The UNIT parameter specifies:
v The location of data records in physical files.
v The access path for both physical files and logical files.
If you specify a unit number and also an auxiliary storage pool, the unit number is
ignored. For more information about auxiliary storage pools, see the Backup and
Recovery book.
If you do not specify the type of maintenance for a file, the default is immediate
maintenance.
You should use delayed maintenance for files that have relatively few changes to
the access path while the file members are closed. Delayed maintenance reduces
system overhead by reducing the number of access paths that are maintained
immediately. It may also result in faster open processing, because the access paths
do not have to be rebuilt.
You may want to specify immediate maintenance for access paths that are used
frequently, or when you cannot wait for an access path to be rebuilt when the file
is opened. You may want to specify delayed maintenance for access paths that are
not used frequently, if infrequent changes are made to the record keys that make
up the access path.
| For more information about recovering your data, see “Chapter 14. Recovering and
| restoring your database” on page 215.
Table 2 shows your choices for possible combinations of duplicate key and
maintenance options.
Table 2. Recovery Options
With This Duplicate And This
Key Option Maintenance Option Your Recovery Options Are
Unique Immediate Rebuild during the IPL (*IPL) Rebuild after
the IPL (*AFTIPL, default) Do not rebuild at
IPL, wait for first open (*NO)
Not unique Immediate or Rebuild during the IPL (*IPL) Rebuild after
delayed the IPL (*AFTIPL) Do not rebuild at IPL,
wait for first open (*NO, default)
Not unique Rebuild Do not rebuild at IPL, wait for first open
(*NO, default)
| For more information about recovering data, see Backup and Recovery
For more information about sharing files in the same job, see “Sharing Database
Files in the Same Job or Activation Group” on page 108.
You can specify any language identifier supported on your system, or you can
specify that the language identifier for the current job be used.
For information about describing a physical file record format, see “Example:
Describing a physical file using DDS” on page 7.
For information about describing a physical file access path, refer to “Describing
the access path for a database file” on page 17.
The following command creates a one-member file using DDS and places it in a
library called DSTPRODLB.
CRTPF FILE(DSTPRODLB/ORDHDRP)
TEXT('Order header physical file')
As shown, this command uses defaults. For the SRCFILE and SRCMBR
parameters, the system uses DDS in the source file called QDDSSRC and the
member named ORDHDRP (the same as the file name). The file ORDHDRP with
one member of the same name is placed in the library DSTPRODLB.
R + (I * N)
where:
R is the starting record count
I is the number of records (increment) to add each time
N is the number of times to add the increment
For example, assume that R is a file created for 5000 records plus 3 increments of
1000 records each. The system can add 1000 to the initial record count of 5000
three times to make the total maximum 8000. When the total maximum is reached,
the system operator either stops the job or tells the system to add another
increment of records and continue. When increments are added, a message is sent
to the system history log. When the file is extended beyond its maximum size, the
minimum extension is 10% of the current size, even if this is larger than the
specified increment.
Instead of taking the default size or specifying a size, you can specify *NOMAX.
For information about the maximum number of records allowed in a file, see
Appendix A. Database File Sizes.
Note: When a physical file is first created, the system always tries to allocate its
initial storage contiguously. The only difference between using
CONTIG(*NO) and CONTIG(*YES) is that with CONTIG(*YES) the system
sends a message to the job log if it is unable to allocate contiguous storage
when the file is created. No message is sent when a file is extended after
creation, regardless of what you specified on the CONTIG parameter.
Note: See “Database file processing: Reusing Deleted Records” on page 103 for
more information on reusing deleted records.
*NO is the default for the REUSEDLT parameter.
If the source type is changed, it is only reflected when the member is subsequently
opened; members currently open are not affected.
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A
A R ORDDTL PFILE(ORDDTLP)
A
2. In the following example, you describe your own record format by listing the
field names you want to include. You can specify the field names in a different
order, rename fields using the RENAME keyword, combine fields using the
CONCAT keyword, and use specific positions of a field using the SST keyword.
You can also override attributes of the fields by specifying different attributes
in the logical file.
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A
A R ORDHDR PFILE(ORDHDRP)
A ORDER
A CUST
A SHPVIA
A
3. In the following example, the file name specified on the FORMAT keyword is
the name of a database file. The record format is shared from this database file
by the logical file being described. The file name can be qualified by a library
name. If a library name is not specified, the library list is used to find the file.
The file must exist when the file you are describing is created. In addition, the
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A
A R CUSRCD PFILE(CUSMSTP)
A FORMAT(CUSMSTL)
A
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A
A R LOGREC PFILE(PF1)
A D 10S 0
A A
A C 5S 0
A
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A
A R PHYREC
A A 8S 2
A B 32
A C 2B 0
A D 10
A
When a record is read from the logical file, the fields from the physical file are
changed to match the logical file description. If the program updates or adds a
record, the fields are changed back. For an add or update operation using a logical
file, the program must supply data that conforms with the format used by the
logical file.
The following chart shows what types of data mapping are valid between physical
and logical files.
Note: For information about mapping DBCS fields, see Appendix B. Double-Byte
Character Set (DBCS) Considerations.
Note: The usage value (in position 38) is not used on a reference function. When
another file refers to a field (using a REF or REFFLD keyword) in a logical
file, the usage value is not copied into that file.
If your program updates a record in which you have specified input-only fields,
the input-only fields are not changed in the file. If your program adds a record
that has input-only fields, the input-only fields take default values (DFT keyword).
Use neither fields when the attributes of join fields in the physical files do not
match. In this case, one or both join fields must be defined again. However, you
cannot include these redefined fields in the record format (the application program
does not see the redefined fields.) Therefore, redefined join fields can be coded N
so that they do not appear in the record format.
A field with N in position 38 does not appear in the buffer used by your program.
However, the field description is displayed with the Display File Field Description
(DSPFFD) command.
For an example of a neither field, see “Describing Fields That Never Appear in the
Record Format (Example 5)” on page 73.
Concatenated Fields
Using the CONCAT keyword, you can combine two or more fields from a physical
file record format to make one field in a logical file record format. For example, a
physical file record format contains the fields Month, Day, and Year. For a logical
file, you concatenate these fields into one field, Date.
The field length for the resulting concatenated field is the sum of the lengths of the
included fields (unless the fields in the physical file are binary or packed decimal,
in which case they are changed to zoned decimal). The field length of the resulting
field is automatically calculated by the system. A concatenated field can have:
v Column headings
Note: This editing and validity checking information is not used by the database
management system but is retrieved when field descriptions from the
database file are referred to in a display or printer file.
When fields are concatenated, the data types can change (the resulting data type is
automatically determined by the system). The following rules and restrictions
apply:
v The OS/400 program assigns the data type based on the data types of the fields
that are being concatenated.
v The maximum length of a concatenated field varies depending on the data type
of the concatenated field and the length of the fields being concatenated. If the
concatenated field is zoned decimal (S), its total length cannot exceed 31 bytes; if
it is character (A), its total length cannot exceed 32 766 bytes.
v In join logical files, the fields to be concatenated must be from the same physical
file. The first field specified on the CONCAT keyword identifies which physical
file is to be used. The first field must, therefore, be unique among the physical
files on which the logical file is based, or you must also specify the JREF
keyword to specify which physical file to use.
v The use of a concatenated field must be I (input only) if the concatenated field is
variable length. Otherwise, the use may be B (both input and output).
v REFSHIFT cannot be specified on a concatenated field that has been assigned a
data type of O or J.
v If any of the fields contain the null value, the result of concatenation is the null
value.
The following shows the field description in DDS for concatenation. (The CONCAT
keyword is used to specify the fields to concatenate.)
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A
00101A MONTH
00102A DAY
00103A YEAR
00104A DATE CONCAT(MONTH DAY YEAR)
A
In this example, the logical file record format includes the separate fields of Month,
Day, and Year, as well as the concatenated Date field. Any of the following can be
used:
v A format with the separate fields of Month, Day, and Year
When both separate and concatenated fields exist in the format, any updates to the
fields are processed in the sequence in which the DDS is specified. In the previous
example, if the Date field contained 103188 and the Month field is changed to 12,
when the record is updated, the month in the Date field would be used. The
updated record would contain 103188. If the Date field were specified first, the
updated record would contain 123188.
Concatenated fields can also be used as key fields and select/omit fields.
Substring Fields
You can use the SST keyword to specify which fields (character, hexadecimal, or
zoned decimal) are in a substring. (You can also use substring with a packed field
in a physical file by specifying S (zoned decimal) as the data type in the logical
file.) For example, assume you defined the Date field in physical file PF1 as 6
characters in length. You can describe the logical file with three fields, each 2
characters in length. You can use the SST keyword to define MM as 2 characters
starting in position 1 of the Date field, DD as 2 characters starting in position 3 of
the Date field, and YY as 2 characters starting in position 5 of the Date field.
The following shows the field descriptions in DDS for these substring fields. The
SST keyword is used to specify the field to substring.
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A R REC1 PFILE(PF1)
A
A MM I SST(DATE 1 2)
A DD I SST(DATE 3 2)
A YY I SST(DATE 5 2)
A
Note that the starting position of the substring is specified according to its position
in the field being operated on (Date), not according to its position in the file. The I
in the Usage column indicates input-only.
Substring fields can also be used as key fields and select/omit fields.
Renamed Fields
You can name a field in a logical file differently than in a physical file using the
RENAME keyword. You might want to rename a field in a logical file because the
program was written using a different field name or because the original field
name does not conform to the naming restrictions of the high-level language you
are using.
Translated Fields
You can specify a translation table for a field using the TRNTBL keyword. When
you read a logical file record and a translation table was specified for one or more
fields in the logical file, the system translates the data from the field value in the
physical file to the value determined by the translation table.
You can inadvertently change the value of a field which your program did not
explicitly change. For floating-point fields, this can occur if a physical file has a
double-precision field that is mapped to a single-precision field in a logical file,
and you issue an update for the record through the logical file. If the internal
representation of the floating-point number causes it to be rounded when it is
mapped to the logical file, then the update of the logical record causes a
permanent loss of precision in the physical file. If the rounded number is the key
of the physical record, then the sequence of records in the physical file can also
change.
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A R CUSRCD PFILE(CUSMSTP)
A K ARBAL
A K CRDLMT
A
2. Encoded vector access path specification. You define the encoded vector access
path with the SQL CREATE INDEX statement.
3. Arrival sequence access path specification. Specify no key fields. You can
specify only one physical file on the PFILE keyword (and only one of the
physical file’s members when you add the logical file member).
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A R CUSRCD PFILE(CUSMSTP)
Note: Even though the specified file’s access path specifications are used, the
system determines which file’s access path, if any, will actually be
shared. The system always tries to share access paths, regardless of
whether the REFACCPTH keyword is used.
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
REFACCPTH(DSTPRODLIB/ORDHDRL)
A R CUSRCD PFILE(CUSMSTP)
When you define a record format for a logical file that shares key field
specifications of another file’s access path (using the DDS keyword, REFACCPTH),
you can use any fields from the associated physical file record format. These fields
do not have to be used in the file that describes the access path. However, all key
and select/omit fields used in the file that describes the access path must be used
in the new record format.
Note: Select/omit specifications appear after key specifications (if keys are
specified).
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A S ITMNBR VALUES(301542 306902 382101 422109 +
A 431652 486592 502356 556608 590307)
A
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A S ITMNBR RANGE(301000 599999)
A
v CMP. The contents of a field are compared to a value or the contents of another
field. Valid comparison codes are EQ, NE, LT, NL, GT, NG, LE, and GE. If the
comparison is met, the record is selected or omitted. In the following example, a
record is selected if its Itmnbr field is less than or equal to 599999:
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A S ITMNBR CMP(LE 599999)
A
The value for a numeric field for which the CMP, VALUES, or RANGE keyword is
specified is aligned based on the decimal positions specified for the field and filled
with zeros where necessary. If decimal positions were not specified for the field,
the decimal point is placed to the right of the farthest right digit in the value. For
example, for a numeric field with length 5 and decimal position 2, the value 1.2 is
interpreted as 001.20 and the value 100 is interpreted as 100.00.
Normally the select and omit comparisons are treated independently from one
another; the comparisons are ORed together. That is, if the select or omit
comparison is met, the record is either selected or omitted. If the condition is not
met, the system proceeds to the next comparison. To connect comparisons together,
you simply leave a space in position 17 of the DDS Form. Then, all the
comparisons that were connected in this fashion must be met before the record is
selected or omitted. That is, the comparisons are ANDed together.
The fewer comparisons, the more efficient the task is. So, when you have several
select/omit comparisons, try to specify the one that selects or omits the most
records first.
In the following examples, few records exist for which the Rep field is JSMITH. The
examples show how to use DDS to select all the records before 1988 for a sales
representative named JSMITH in the state of New York. All give the same results
with different efficiency (in this example, 3 is the most efficient).
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A O YEAR CMP(GE 88) 2
A S ST CMP(EQ 'NY')
A REP CMP(EQ 'JSMITH')
A
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A O REP CMP(NE 'JSMITH') 3
A O ST CMP(NE 'NY')
A S YEAR CMP(LT 88)
A
1 All records must be compared with all of the select fields St, Rep, and Year
before they can be selected or omitted.
2 All records are compared with the Year field. Then, the records before 1988
have to be compared with the St and Rep fields.
3 All records are compared with the Rep field. Then, only the few for
JSMITH are compared with the St field. Then, the few records that are left
are compared to the Year field.
If you create the preceding example with a sort sequence table, the select/omit
fields are translated according to the sort table before the comparison. For example,
with a sort sequence table using shared weightings for uppercase and lowercase,
NY and ny are equal. For details, see DDS Reference.
The following shows how to code this example using the DDS select and omit
functions:
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A S DPTNBR CMP(NE 12)
A S ITMNBR VALUES(112505 428707 480100)
A
It is possible to have an access path with select/omit values and process the file in
arrival sequence. For example, a high-level language program can specify that the
keyed access path is to be ignored. In this case, every record is read from the file in
arrival sequence, but only those records meeting the select/omit values specified in
the file are returned to the high-level language program.
A logical file with key fields and select/omit values specified can be processed in
arrival sequence or using relative record numbers randomly. Records omitted by
the select/omit values are not processed. That is, if an omitted record is requested
by relative record number, the record is not returned to the high-level language
program.
The system does not ensure that any additions or changes through a logical file
will allow the record to be accessed again in the same logical file. For example, if
the selection values of the logical file specifies only records with an A in Fld1 and
the program updates the record with a B in Fld1, the program cannot retrieve the
record again using this logical file.
Note: You cannot select or omit based on the values of a floating-point field.
The two kinds of select/omit operations are: access path select/omit and dynamic
select/omit. The default is access path select/omit. The select/omit specifications
themselves are the same in each kind, but the system actually does the work of
selecting and omitting records at different times.
Dynamic Select/Omit
With dynamic select/omit, when a program reads records from the file, the system
only returns those records that meet the select/omit values. That is, the actual
select/omit processing is done when records are read by a program, rather than
when the records are added or changed. However, the keyed sequence access path
contains all the keys, not just keys from selected records. Access paths using
dynamic select/omit allow more access path sharing, which can improve
performance. For more information about access path sharing, see “Using Existing
Access Paths”.
If you have a file that is updated frequently and read infrequently, you may not
need to update the access path for select/omit purposes until your program reads
the file. In this case, dynamic select/omit might be the correct choice. The
following example helps describe this.
When a logical file with a keyed sequence access path is created, the system
always tries to share an existing access path. For access path sharing to occur, an
access path must exist on the system that satisfies the following conditions:
v The logical file member to be added must be based on the same physical file
members that the existing access path is based on.
Note: Logical files that contain concatenated or substring fields cannot share access
paths with physical files.
The owner of any access path is the logical file member that originally created the
access path. For a shared access path, if the logical member owning the access path
is deleted, the first member to share the access path becomes the new owner. The
FRCACCPTH, MAINT, and RECOVER parameters on the Create Logical File
(CRTLF) command need not match the same parameters on an existing access path
for that access path to be shared. When an access path is shared by several logical
file members, and the FRCACCPTH, MAINT, and RECOVER parameters are not
identical, the system maintains the access path by the most restrictive value for
each of the parameters specified by the sharing members. The following illustrates
Access path sharing does not depend on sharing between members; therefore, it
does not restrict the order in which members can be deleted.
The Display File Description (DSPFD) and Display Database Relations (DSPDBR)
commands show access path sharing relationships.
Two logical files, LFILE1 and LFILE2, are built over the physical file PFILE.
LFILE1, which was created first, has two key fields, KFD1 and KFD2. LFILE2 has
three key fields, KFD1, KFD2, and KFD3. The two logical files use two of the same
key fields, but no access path is shared because the logical file with three key fields
was created after the file with two key fields.
Table 3. Physical and Logical Files Before Save and Restore
Physical File (PFILE) Logical File 1 (LFILE1) Logical File 2 (LFILE2)
Access Path KFD1, KFD2 KFD1, KFD2, KFD3
Fields KFD1, KFD2, KFD3, A, KFD1, KFD2, KFD3, F, KFD1, KFD2, KFD3, D,
B, C, D, E, F, G C, A G, F, E
An application uses LFILE1 to access the records and to change the KFD3 field to
blank if it contains a C, and to a C if it is blank. This application causes the user
no unexpected results because the access paths are not shared. However, after a
save and restore of the physical file and both logical files, the program appears to
do nothing and takes longer to process.
Because it has three key fields, LFILE2 is restored first. After recovery, LFILE1
implicitly shares the access path for LFILE2. Users who do not understand
implicitly shared access paths do not realize that when they use LFILE1 after a
recovery, they are really using the key for LFILE2.
The first record is read via the first key of 0101<blank> and changed to 0101C. The
records now look like:
When the application issues a get next key, the next higher key above 0101<blank>
is 0101C. This is the record that was just changed. However, this time the
application changes the KFD3 field from C to blank.
Because the user does not understand implicit access path sharing, the application
accesses and changes every record twice. The end result is that the application
takes longer to run, and the records look like they have not changed.
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A* ORDER HEADER LOGICAL FILE (ORDHDRL)
A R ORDHDR PFILE(ORDHDRP)
A K ORDER
This file uses the key field Order (order number) to define the access path. The
record format is the same as the associated physical file ORDHDRP. The record
format name for the logical file must be the same as the record format name in
the physical file because no field descriptions are given.
2. Create the logical file. You can use the Create Logical File (CRTLF) command.
The following shows how the CRTLF command could be typed:
As shown, this command uses some defaults. For example, because the SRCFILE
and SRCMBR parameters are not specified, the system used DDS from the
IBM-supplied source file QDDSSRC, and the source file member name is
ORDHDRL (the same as the file name specified on the CRTLF command). The file
ORDHDRL with one member of the same name is placed in the library
DSTPRODLB.
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A* ORDER DETAIL FILE (ORDDTLP) - PHYSICAL FILE RECORD DEFINITION
A REF(DSTREF)
A R ORDDTL TEXT('Order detail record')
A CUST R
A ORDER R
A LINE R
A ITEM R
A QTYORD R
A DESCRP R
A PRICE R
A EXTENS R
A WHSLOC R
A ORDATE R
A CUTYPE R
A STATE R
A ACTMTH R
A ACTYR R
A
Figure 10. DDS for a Physical File (ORDDTLP) Built from a Field Reference File
Figure 11. DDS for a Physical File (ORDHDRP) Built from a Field Reference File
The following example shows how to create a logical file ORDFILL with two
record formats. One record format is defined for order header records from the
physical file ORDHDRP; the other is defined for order detail records from the
physical file ORDDTLP. ( Figure 10 on page 54 shows the DDS for the physical file
ORDDTLP, Figure 11 shows the DDS for the physical file ORDHDRP, and Figure 12
shows the DDS for the logical file ORDFILL.)
The logical file record format ORDHDR uses one key field, Order, for sequencing;
the logical file record format ORDDTL uses two keys fields, Order and Line, for
sequencing.
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A* ORDER TRANSACTION LOGICAL FILE (ORDFILL)
A R ORDHDR PFILE(ORDHDRP)
A K ORDER
A
A R ORDDTL PFILE(ORDDTLP)
A K ORDER
A K LINE
A
To create the logical file ORDFILL with two associated physical files, use a Create
Logical File (CRTLF) command like the following:
CRTLF FILE(DSTPRODLB/ORDFILL)
TEXT('Order transaction logical file')
The DDS source is in the member ORDFILL in the file QDDSSRC. The file
ORDFILL with a member of the same name is placed in the DSTPRODLB library.
The access path for the logical file member ORDFILL arranges records from both
the ORDHDRP and ORDDTLP files. Record formats for both physical files are
keyed on Order as the common field. Because of the order in which they were
specified in the logical file description, they are merged in Order sequence with
duplicates between files retrieved first from the header file ORDHDRP and second
Note: In certain circumstances, it is better to use multiple logical files, rather than
to use a multiple-format logical file. For example, when keyed access is used
with a multiple-format logical file, it is possible to experience poor
performance if one of the files has very few records. Even though there are
multiple formats, the logical file has only one index, with entries from each
physical file. Depending on the kind of processing being done by the
application program (for example, using RPG SETLL and READE with a key
to process the small file), the system might have to search all index entries
in order to find an entry from the small file. If the index has many entries,
searching the index might take a long time, depending on the number of
keys from each file and the sequence of keys in the index. (If the small file
has no records, performance is not affected, because the system can take a
fast path and avoid searching the index.)
In DDS, the header record format is defined before the detail record format. If the
access path uses the Order field as the first key field for both record formats and
the Line field as the second key field for only the second record format, both in
ascending sequence, the order of the records in the access path is:
Record 2
Record A
Record D
Record B
Record 1
Record E
Record C
Note: Records with duplicate key values are arranged first in the sequence in
which the physical files are specified. Then, if duplicates still exist within a
record format, the duplicate records are arranged in the order specified by
the FIFO, LIFO, or FCFO keyword. For example, if the logical file specified
56 DB2 UDB for AS/400 Database Programming V4R5
the DDS keyword FIFO, then duplicate records within the format would be
presented in first-in-first-out sequence.
For logical files with more than one record format, you can use the *NONE DDS
function for key fields to separate records of one record format from records of
other record formats in the same access path. Generally, records from all record
formats are merged based on key values. However, if *NONE is specified in DDS
for a key field, only the records with key fields that appear in all record formats
before the *NONE are merged.
The logical file in the following example contains three record formats, each
associated with a different physical file:
Note: All record formats have one key field in common, the Empnbr field.
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A
A K EMPNBR 1
A
A K EMPNBR 2
A K EMPDAT
A
A K EMPNBR 3
A K *NONE
A K CLSNBR
A
*NONE is assumed for the second and third key fields for EMPMSTR and the
third key field for EMPHIST because no key fields follow these key field positions.
*NONE serves as a separator for the record formats EMPHIST and EMPEDUC. All
the records for EMPHIST with the same Empnbr field are grouped together and
sorted by the Empdat field. All the records for EMPEDUC with the same Empnbr
field are grouped together and sorted by the Clsnbr field.
Note: Because additional key field values are placed in the key sequence access
path to guarantee the above sequencing, duplicate key values are not
predictable.
When you add records to a multiple-format logical file and your application
program uses a file name instead of a record format name, you need to write a
format selector program. For more information about format selector programs, see
“Identifying Which Record Format to Add in a File with Multiple Formats” on
page 184.
The record formats used with all logical members in a logical file must be defined
in DDS when the file is created. If new record formats are needed, another logical
file or record format must be created.
When a logical file is created or a member is added to the file, you can use the
DTAMBRS parameter on the Create Logical File (CRTLF) or the Add Logical File
Member (ADDLFM) command to specify which members of the physical files used
by the logical file are to be used for data. *NONE can be specified as the physical
file member name if no members from a physical file are to be used for data.
In the following example, the logical file has two record formats defined:
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A
00010A R LOGRCD2 PFILE(PF1 PF2)
A .
A .
A .
00020A R LOGRCD3 PFILE(PF1 PF2 PF3)
A .
A .
A .
A
Record format LOGRCD2 is associated with physical file member M1 in PF1 and
M1 and M2 in file PF2. Record format LOGRCD3 is associated with M1 in PF1 and
M3 in PF3. No members in PF2 are associated with LOGRCD3. If the same
physical file name is specified on more than one PFILE keyword, each occurrence
of the physical file name is handled as a different physical file.
If a library name is not specified for the file on the PFILE keyword, the library list
is used to find the physical file when the logical file is created. The physical file
name and the library name then become part of the logical file description. The
physical file names and the library names specified on the DTAMBRS parameter
must be the same as those stored in the logical file description.
If a file name is not qualified by a library name on the DTAMBRS parameter, the
library name defaults to *CURRENT, and the system uses the library name that is
stored in the logical file description for the respective physical file name. This
library name is either the library name that was specified for the file on the PFILE
DDS keyword or the name of the library in which the file was found using the
library list when the logical file was created.
When you add a member to a logical file, you can specify data members as
follows:
v Specify no associated physical file members (DTAMBRS (*ALL) default). The
logical file member is associated with all the physical file members of all
physical files in all the PFILE keywords specified in the logical file DDS.
v Specify the associated physical file members (DTAMBRS parameter). If you do
not specify library names, the logical file determines the libraries used. When
more than one physical file member is specified for a physical file, the member
names should be specified in the order in which records are to be retrieved
You can use the Create Logical File (CRTLF) command to create the first member
when you create the logical file. Subsequent members must be added using the
Add Logical File Member (ADDLFM) command. However, if you are going to add
more members, you must specify more than 1 for the MAXMBRS parameter on the
CRTLF command. The following example of adding a member to a logical file uses
the CRTLF command used earlier in “Creating a Logical File” on page 53.
CRTLF FILE(DSTPRODLB/ORDHDRL)
MBR(*FILE) DTAMBRS(*ALL)
TEXT('Order header logical file')
*FILE is the default for the MBR parameter and means the name of the member is
the same as the name of the file. All the members of the associated physical file
(ORDHDRP) are used in the logical file (ORDHDRL) member. The text description
is the text description of the member.
In general, the examples in this section include a picture of the files, DDS for the
files, and sample data. For Example 1, several cases are given that show how to
join files in different situations (when data in the physical files varies).
In the examples, for convenience and ease of recognition, join logical files are
shown with the label JLF, and physical files are illustrated with the labels PF1, PF2,
PF3, and so forth.
The following example illustrates a join logical file that joins two physical files.
This example is used for the five cases discussed in Example 1.
In this example, employee number is common to both physical files (PF1 and PF2),
but name is found only in PF1, and salary is found only in PF2.
With a join logical file, the application program does one read operation (to the
record format in the join logical file) and gets all the data needed from both
physical files. Without the join specification, the logical file would contain two
record formats, one based on PF1 and the other based on PF2, and the application
program would have to do two read operations to get all the needed data from the
two physical files. Thus, join provides more flexibility in designing your database.
PF1
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A R REC1
A NBR 10
A NAME 20
A K NBR
A
PF2
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A R REC2
A NBR 10
A SALARY 7 2
A K NBR
A
The following describes the DDS for the join logical file in Example 1 (see the DDS
Reference for more information on the specific keywords):
The record level specification identifies the record format name used in the join
logical file.
R Identifies the record format. Only one record format can be placed in a join
logical file.
JFILE Replaces the PFILE keyword used in simple and multiple-format logical
files. You must specify at least two physical files. The first file specified on
the JFILE keyword is the primary file. The other files specified on the
JFILE keyword are secondary files.
The join specification describes the way a pair of physical files is joined. The
second file of the pair is always a secondary file, and there must be one join
specification for each secondary file.
J Identifies the start of a join specification. You must specify at least one join
specification in a join logical file. A join specification ends at the first field
name specified in positions 19 through 28 or at the next J specified in
position 17.
JOIN Identifies which two files are joined by the join specification. If only two
physical files are joined by the join logical file, the JOIN keyword is
optional. See “Joining Three or More Physical Files (Example 7)” on
page 76 later in this section for an example of how to use this keyword.
JFLD Identifies the join fields that join records from the physical files specified
on the JOIN. JFLD must be specified at least once for each join
specification. The join fields are fields common to the physical files. The
The field level specification identifies the fields included in the join logical file.
Field names Specifies which fields (in this example, Nbr, Name, and Salary) are
used by the application program. At least one field name is
required. You can specify any field names from the physical files
used by the logical file. You can also use keywords like RENAME,
CONCAT, or SST as you would in simple and multiple format
logical files.
JREF In the record format (which follows the join specification level and
precedes the key field level, if any), the field names must uniquely
identify which physical file the field comes from. In this example,
the Nbr field occurs in both PF1 and PF2. Therefore, the JREF
keyword is required to identify the file from which the Nbr field
description will be used.
The key field level specification is optional, and includes the key field names for
the join logical file.
K Identifies a key field specification. The K appears in position 17.
Key field specifications are optional.
Key field names
Key field names (in this example, Nbr is the only key field) are
optional and make the join logical file an indexed (keyed sequence)
file. Without key fields, the join logical file is an arrival sequence
file. In join logical files, key fields must be fields from the primary
file, and the key field name must be specified in positions 19
through 28 in the logical file record format.
The select/omit field level specification is optional, and includes select/omit field
names for the join logical file.
S or O Identifies a select or omit specification. The S or O appears in
position 17. Select/omit specifications are optional.
Select/omit field names
Only those records meeting the select/omit values will be returned
to the program using the logical file. Select/omit fields must be
specified in positions 19 through 28 in the logical file record
format.
Note: If the DFT keyword is specified in the secondary file, the value specified
for the DFT keyword is used in the join. The result would be at least one
join record for each primary record.
v If a record exists in the secondary file, but the primary file has no matching
value, no record is returned to your program. A second join logical file can be
used that reverses the order of primary and secondary files to determine if
secondary file records exist with no matching primary file records.
Note: When the JDFTVAL is not specified, the system returns a record only if a
match is found in every secondary file for a record in the primary file.
The program does four read operations and gets the following records:
With the join logical file shown in Example 1, the program reads the join logical
file and gets the following records:
If you do not specify the JDFTVAL keyword and no match is found for the join
field in the secondary file, the record is not included in the join logical file.
The program reads the join logical file and gets the following records:
With JDFTVAL specified, the system returns a record for 500, even though the
record is missing in PF2. Without that record, some field values can be missing in
the join record (in this case, the Salary field is missing). With JDFTVAL specified,
missing character fields normally use blanks; missing numeric fields use zeros.
However, if the DFT keyword is specified for the field in the physical file, the
default value specified on the DFT keyword is used.
Secondary File Has More Than One Match for a Record in the
Primary File (Case 3)
Assume that a join logical file is specified as in Figure 13 on page 62, and that four
records in PF1 and five records in PF2, as follows:
The program reads the join logical file and gets only four records, which would be
the same even if JDFTVAL was specified (because a record must always be
contained in the primary file to get a join record):
Given a value of 500 from the program for the Nbr field in the logical file and with
the JDFTVAL keyword specified, the system supplies the following record:
Note: If the JDFTVAL keyword was not specified in the join logical file, no record
would be found for a value of 500 because no matching record is contained
in the secondary file.
Given a value of 984 from the program for the Nbr field in the logical file, the
system supplies no record and a no record found exception occurs because record
984 is not in the primary file.
Given a value of 997 from the program for the Nbr field in the logical file, the
system returns one of the following records:
Note: You can specify the field names in any order. If the same field names
appear in different physical files, specify the name of the physical file on
PF1
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A R REC1
A PTNBR 4
A COLOR 20
A PRICE 7 2
A VENDOR 40
A
PF2
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A R REC2
A PTNBR 4
A COLOR 20
A QUANTOH 5 0
A WAREHSE 30
A
If the file is processed sequentially, the program receives the following records:
Note that no record for part number 190, color blue, is available to the program,
because a match was not found on both fields in the secondary file. Because
JDFTVAL was not specified, no record is returned.
The DDS for the physical files and for the join logical file are as follows:
JLF
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A R JREC JFILE(PF1 PF2)
A J JOIN(PF1 PF2)
A JFLD(NAME1 NAME2)
A JDUPSEQ(TELEPHONE)
A NAME1
A ADDR
A TELEPHONE
A
PF1
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A R REC1
A NAME1 10
A ADDR 20
A
PF2
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A R REC2
A NAME2 10
A TELEPHONE 8
A
The program reads all the records available for Anne, then Doug, then Mark. Anne
has one address, but three telephone numbers. Therefore, there are three records
returned for Anne.
The records for Anne sort in ascending sequence by telephone number because the
JDUPSEQ keyword sorts in ascending sequence unless you specify *DESCEND as
JLF
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A R JREC JFILE(PF1 PF2)
A J JOIN(PF1 PF2)
A JFLD(NAME1 NAME2)
A JDUPSEQ(TELEPHONE *DESCEND)
A NAME1
A ADDR
A TELEPHONE
A
When you specify JDUPSEQ with *DESCEND, the records are returned as follows:
Note: The JDUPSEQ keyword applies only to the join specification in which it is
specified. For an example showing the JDUPSEQ keyword in a join logical
file with more than one join specification, see “A Complex Join Logical File
(Example 10)” on page 81.
You can also use character type fields that have different lengths as join fields
without requiring any redefinition of the fields. For example, if the NAME1 Field
of PF1 was 10 characters long and the NAME2 field of PF2 was 15 characters long,
those fields could be used as join fields without redefining one of the fields.
The following is an example in which the join fields do not have the same
attributes. The Nbr field in physical file PF1 and the Nbr field in physical file PF2
both have a length of 3 specified in position 34, but in the PF1 file the field is
zoned (S in position 35), and in the PF2 file the field is packed (P in position 35).
To join the two files using these fields as join fields, you must redefine one or both
fields to have the same attributes.
JLF
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A R JOINREC JFILE(PF1 PF2)
A J JOIN(PF1 PF2)
A JFLD(NBR NBR)
A NBR S JREF(2)
A NAME
A SALARY
A
PF1
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A R REC1
A NBR 3S 0 <-Zoned
A NAME 20
A K NBR
A
PF2
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A R REC2
A NBR 3P 0 <-Packed
A SALARY 7 2
A K NBR
A
Note: In this example, the Nbr field in the logical file comes from PF2, because
JREF(2) is specified. Instead of specifying the physical file name, you can
specify a relative file number on the JREF keyword; in this example, the 2
indicates PF2.
Because the Nbr fields in the PF1 and PF2 files are used as the join fields, they
must have the same attributes. In this example, they do not. Therefore, you must
redefine one or both of them to have the same attributes. In this example, to
resolve the difference in the attributes of the two employee number fields, the Nbr
field in JLF (which is coming from the PF2 file) is redefined as zoned (S in position
35 of JLF).
In the following example, the program reads the descriptions, prices, and quantity
on hand of parts in stock. The part numbers themselves are not wanted except to
bring together the records of the parts. However, because the part numbers have
different attributes, at least one must be redefined.
JLF
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A R JOINREC JFILE(PF1 PF2)
A J JOIN(PF1 PF2)
A JFLD(PRTNBR PRTNBR)
A PRTNBR S N JREF(1)
A DESC
A PRICE
A QUANT
A K DESC
A
PF1
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A R REC1
A DESC 30
A PRTNBR 6P 0
A
PF2
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A R REC2
A PRTNBR 6S 0
A PRICE 7 2
A QUANT 8 0
A
In PF1, the Prtnbr field is a packed decimal field; in PF2, the Prtnbr field is a zoned
decimal field. In the join logical file, they are used as join fields, and the Prtnbr
field from PF1 is redefined to be a zoned decimal field by specifying an S in
position 35 at the field level. The JREF keyword identifies which physical file the
field comes from. However, the field is not included in the record format;
therefore, N is specified in position 38 to make it a neither field. A program using
this file would not see the field.
In this example, a sales clerk can type a description of a part. The program can
read the join logical file for a match or a close match, and display one or more
parts for the user to examine, including the description, price, and quantity. This
JLF
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A R JOINREC JFILE(PF1 PF2)
A J JOIN(PF1 PF2)
A JFLD(NBR NUMBER)
A JFLD(FLD3 FLD31)
A FLD1 RENAME(F1)
A FLD2 JREF(2)
A FLD3 35 N
A NAME
A TELEPHONE CONCAT(AREA LOCAL)
A K FLD1
A K NAME
A
PF1
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A R REC1
A NBR 4
A F1 20
A FLD2 7 2
A FLD3 40
A NAME 20
A
PF2
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A R REC2
A NUMBER 4
A FLD2 7 2
A FLD31 35
A AREA 3
A LOCAL 7
A
The physical files must be joined in pairs, with each pair described by a join
specification. Each join specification must have one or more join fields identified.
The following shows the fields in the files and one field common to all the
physical files in the logical file:
In this example, the Name field is common to all the physical files (PF1, PF2, and
PF3), and serves as the join field.
The following shows the DDS for the physical and logical files:
PF1
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A R REC1
A NAME 10
A ADDR 20
A K NAME
A
PF2
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A R REC2
A NAME 10
A TELEPHONE 7
A K NAME
A
PF3
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A R REC3
A NAME 10
A SALARY 9 2
A K NAME
A
No record is returned for Tom because a record is not found for him in PF2 and
PF3 and the JDFTVAL keyword is not specified. No record is returned for Sue
because the primary file has no record for Sue.
JLF
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A JDFTVAL
A R JOINREC JFILE(PF1 PF1)
A J JOIN(1 2)
A JFLD(MGRNBR NBR)
A NBR JREF(1)
A NAME JREF(1)
A MGRNAME RENAME(NAME)
A JREF(2)
A
PF1
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A R RCD1
A NBR 3
A NAME 10 DFT('none')
A MGRNBR 3
A
Notes:
1. Relative file numbers must be specified on the JOIN keyword because the same
file name is specified twice on the JFILE keyword. Relative file number 1 refers
to the first physical file specified on the JFILE keyword, 2 refers to the second,
and so forth.
2. With the same physical files specified on the JFILE keyword, the JREF keyword
is required for each field specified at the field level.
Note that a record is returned for the manager name of Sue because the JDFTVAL
keyword was specified. Also note that the value none is returned because the DFT
keyword was used on the Name field in the PF1 physical file.
PF1
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A R RCD1
A NAME 20
A ADDR 40
A COUNTRY 40
A
PF2
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A R RCD2
A NAME 20
A TELEPHONE 8 DFT('999-9999')
A
PF3
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A R RCD3
A TELEPHONE 8
A LOC 30 DFT('No location assigned')
A
Assume that PF1, PF2, and PF3 have the following records:
In this example, complete data is found for Anne and Doug. However, part of the
data is missing for Mark and Sue.
v PF2 is missing a record for Mark because he has no telephone number. The
default value for the Telephone field in PF2 is defined as 999-9999 using the DFT
keyword. In this example, therefore, 999-9999 is the telephone number returned
when no telephone number is assigned. The JDFTVAL keyword specified in the
join logical file causes the default value for the Telephone field (which is 999-9999)
in PF2 to be used to match with a record in PF3. (In PF3, a record is included to
show a description for telephone number 999-9999.) Without the JDFTVAL
keyword, no record would be returned for Mark.
v Sue’s telephone number is not yet assigned a location; therefore, a record for
555-1144 is missing in PF3. Without JDFTVAL specified, no record would be
returned for Sue. With JDFTVAL specified, the system supplies the default value
specified on the DFT keyword in PF3 the Loc field (which is No location
assigned).
The join logical file record format should contain the following fields:
Vdrnam (vendor name)
Street, City, State, and Zipcode (vendor address)
Jobnbr (job number)
Prtnbr (part number)
Descr (description of part)
Qorder (quantity ordered)
Untprc (unit price)
Whsnbr (warehouse number)
Prtloc (location of part)
1 The DYNSLT keyword is required because the JDFTVAL keyword and
select fields are specified.
2 The JDFTVAL keyword is specified to pick up default values in physical
files.
3 First join specification.
4 The JDUPSEQ keyword is specified because duplicate vendor numbers
occur in PF2.
5 Second join specification.
6 Two JFLD keywords are specified to ensure the correct records are joined
from the PF2 and PF3 files.
7 The Vdrnum field is redefined from zoned decimal to character (because it
is used as a join field and it does not have the same attributes in PF1 and
PF2).
8 The CONCAT keyword concatenates four fields from the same physical file
into one field.
9 The JREF keyword must be specified because the Prtnbr field exists in two
physical files and you want to use the one in PF2.
10 The select/omit fields are Vdrnam and Qorder. (Note that they come from
two different physical files.)
Note: Join logical files always have access paths using the second field of the
pair of fields specified in the JFLD keyword. This field acts like a key
field in simple logical files. If an access path does not already exist, the
access path is implicitly created with immediate maintenance.
Note: Relative file numbers must be specified on the JOIN keyword and any
JREF keyword when the same file name is specified twice on the JFILE
keyword.
– Every secondary file must be specified only once as the second file of the pair
of files on the JOIN keyword. This means that for every secondary file on the
| The following topics describe the types of authority you can grant to a user for a
| database file:
| v “Object Operational Authority”
| v “Object Existence Authority”
| v “Object Management Authority” on page 88
| v “Object Alter Authority” on page 88
| v “Using Data Authorities to Grant Users Access to Physical and Logical Files” on
| page 88
| Note: You must also have the appropriate data authorities required by the options
| specified on the open operation.
| Note: All these functions except save/restore also require object operational
| authority to the file.
| Adding a physical file referential constraint checks for either object management
authority or object reference authority to the parent file. Physical file constraints
are described in “Chapter 16. Controlling the integrity of your database with
constraints” on page 237 and “Chapter 17. Ensuring data integrity with referential
constraints” on page 245.
Note: When creating a logical file, no data authorities are granted. Consequently,
*CHANGE is the same as *USE, and *ALL does not grant any data
authority.
You can use the Edit Object Authority (EDTOBJAUT), Grant Object Authority
(GRTOBJAUT), or Revoke Object Authority (RVKOBJAUT) commands to grant or
revoke the public authority of a file.
| When you create a physical file, you can specify if the file is update-capable and
| delete-capable by using the ALWUPD and ALWDLT parameters on the Create
| Physical File (CRTPF) and Create Source Physical File (CRTSRCPF) commands. By
| creating a file that is not update-capable and not delete-capable, you can effectively
| enforce an environment where data cannot be changed or deleted from a file once
| the data is written.
File capabilities cannot be explicitly set for logical files. The file capabilities of a
logical file are determined by the file capabilities of the physical files it is based on.
You can also use a logical file to prevent one or more fields from being changed in
a physical file by specifying, for those fields you want to protect, an I (input only)
in position 38 of the DDS form. For more information about this subject, see
“Describing Field Use for Logical Files” on page 41.
You can use a logical file to secure records in a physical file based on the contents
of one or more fields in that record. To secure records based on the contents of a
field, use the select and omit keywords when describing the logical file. For more
information about this subject, see “Selecting and Omitting Records Using Logical
| Files” on page 46.
| You can use Operations Navigator to quickly and easily design and manage a
| database by using only a mouse. Here are just a few highlights:
| v When creating a new table, you can browse existing tables to find and use your
| favorite column definitions.
| v You can create a joined view by using drag and drop.
| v You can even directly edit your table data.
| v You can use Operations Navigator to create, edit, run, and troubleshoot scripts
| for SQL statements.
| You can learn more about AS/400 database operations and DB2 Universal
| Database for AS/400 by taking a look at the DB2 Universal Database for AS/400
| topic.
|
| Accessing Operations Navigator Database
| If you need to install Operations Navigator, first see Getting started with
| Operations Navigator. When you have enabled and accessed Operations Navigator,
| expand the Database icon for the AS/400 you want to work with. You must have
| the proper authorization to the objects you want to work with in Operations
| Navigator. Otherwise, you will not be authorized to perform any actions on those
| objects even though they are displayed in Operations Navigator.
|
| Performing Tasks in Operations Navigator Database
| There are many tasks you can perform using Operations Navigator Database.
| Operations Navigator is the graphical interface that enables you to work with your
| AS/400 resources. In addition, Operations Navigator makes your administrative
| tasks much easier to complete by integrating the AS/400 operating system with the
| Windows desktop. Listed below are some common database tasks to help you get
| started:
| v “Creating a Library in Operations Navigator Database” on page 94
| v “Creating a Table in Operations Navigator Database” on page 94
| v “Creating a View in Operations Navigator Database” on page 94
| v “Creating an Alias for a Table or View in Operations Navigator Database” on
| page 95
| v “Creating an Index in Operations Navigator Database” on page 95
| Listed below are some advanced database tasks you can perform using Operations
| Navigator Database:
| v “Creating SQL Scripts in Operations Navigator Database” on page 96
| v “Creating an SQL Performance Monitor in Operations Navigator Database” on
| page 97
| For more information about working with libraries within Operations Navigator,
| see the Operations Navigator Database on-line help.
| For more information about views, see the Operations Navigator Database on-line
| help.
| Note: After you specify the name and description of the table to be created,
| you will be prompted to define the columns for the table. While you are
| defining the table, you will also be given the chance to define indexes,
| constraints, and triggers. Creating a table results in a physical file being
| created on the system.
| For more information about indexes, see DB2 UDB for AS/400 SQL Programming
| Concepts and DB2 UDB for AS/400 SQL Reference.
| For more information about indexes, see DB2 UDB for AS/400 SQL Programming
| Concepts and DB2 UDB for AS/400 SQL Reference.
| For more information about procedures, see DB2 UDB for AS/400 SQL
| Programming Concepts and DB2 UDB for AS/400 SQL Reference.
| For more information about functions, see DB2 UDB for AS/400 SQL Programming
| Concepts and DB2 UDB for AS/400 SQL Reference.
| For more information about types, see DB2 UDB for AS/400 SQL Programming
| Concepts and DB2 UDB for AS/400 SQL Reference.
| For more information about SQL scripts, see DB2 UDB for AS/400 SQL
| Programming Concepts.
| Note: You need job-special control authority (*JOBCTL) to use the SQL
| performance monitor. You also need read authority to the QUSRSYS library.
| For more information about SQL performance monitors, see Database Performance
| and Query Optimization.
Another topic covered in this part is sharing database files across jobs so that they
can be accessed by many users at the same time. Locks on files, records, or
members that can prevent them from being shared across jobs are also discussed.
Using the Open Query File (OPNQRYF) command and the Open Database File
(OPNDBF) command to open database file members in a program is discussed.
Examples, performance considerations, and guidelines to follow when writing a
high-level language program are also included. Also, typical errors that can occur
are discussed.
Finally, basic database file operations are discussed. This discussion includes
setting a position in the database file, and reading, updating, adding, and deleting
records in a database file. A description of several ways to read database records is
also included. Information on updating discusses how to change an existing
database record in a logical or physical file. Information on adding a new record to
a physical database member using the write operation is included. This section
also includes ways you can close a database file when your program completes
processing a database file member, disconnecting your program from the file.
Messages to monitor when handling database file errors in a program are also
discussed.
When a file is opened, the attributes in the database file description are merged
with the parameters in the program. Normally, most of the information the system
needs for your program to open and process the file is found in the file attributes
and in the application program itself.
This chapter describes the file processing parameters. The parameter values are
determined by the high-level language program, the file attributes, and any open
or override commands processed before the high-level language program is called.
A summary of these parameters and where you specify them can be found in
“Summary of run time considerations for processing database files” on page 118.
For more information about processing parameters from commands, see the CL
Reference for the following commands:
v Create Physical File (CRTPF)
v Create Logical File (CRTLF)
v Create Source Physical File (CRTSRCPF)
v Add Physical File Member (ADDPFM)
v Add Logical File Member (ADDLFM)
v Change Physical File (CHGPF)
v Change Physical File Member (CHGPFM)
v Change Logical File (CHGLF)
v Change Logical File Member (CHGLFM)
v Change Source Physical File (CHGSRCPF)
v Override with Database File (OVRDBF)
v Open Database File (OPNDBF)
v Open Query File (OPNQRYF)
v Close File (CLOF)
If the member name cannot be specified in the high-level language program (some
high-level languages do not allow a member name), or you want a member other
than the first member, you can use an Override with Database File (OVRDBF)
command or an open command (OPNDBF or OPNQRYF) to specify the file and
member you want to process (using the FILE and MBR parameters).
To process all the members of a file, use the OVRDBF command with the
MBR(*ALL) parameter specified. For example, if FILEX has three members and
you want to process all the members, you can specify:
OVRDBF FILE(FILEX) MBR(*ALL)
If you specify MBR(*ALL) on the OVRDBF command, your program reads the
members in the order they were created. For each member, your program reads the
records in keyed or arrival sequence, depending on whether the file is an arrival
sequence or keyed sequence file.
The system uses the options to determine which operations are allowed in your
program. For example, if you open a file for input only and your program tries an
output operation, your program receives an error.
Normally, the system verifies that you have the required data authority when you
do an input/output operation in your program. However, when you use the Open
Query File (OPNQRYF) or Open Database File (OPNDBF) commands, the system
verifies at the time the file is opened that you have the required data authority to
The system also uses these options to determine the locks to use to protect the data
integrity of the files and records being processed by your program. For more
information on locks, see “Locking shared data” on page 107.
Because of the way the system reuses deleted record space, consider the following
points before creating or changing a file to reuse deleted record space:
v Files processed using relative record numbers and files used by an application to
determine a relative record number that is used as a key into another file should
not reuse deleted record space.
v Files used as queues should not reuse deleted record space.
v Any files used by applications that assume new record inserts are at the end of
the file should not reuse deleted record space.
If you decide to change an existing physical file to reuse deleted record space, and
there are logical files with access paths with LIFO or FIFO duplicate key ordering
over the physical file, you can re-create the logical files without the FIFO or LIFO
attribute and avoid rebuilding the existing access path by doing the following:
1. Rename the existing logical file that has the FIFO or LIFO attribute.
2. Create a second logical file identical to the renamed file except that duplicate
key ordering should not be specified for the file. Give the new file the original
file name. The new file shares the access path of the renamed file.
3. Delete the renamed file.
You can tell the system to ignore the keyed sequence access path in some
high-level languages, or on the Open Database File (OPNDBF) command. When
you ignore the keyed sequence access path, operations that read data by key are
not allowed. Operations are done sequentially along the arrival sequence access
path. (If this option is specified for a logical file with select/omit values defined,
the arrival sequence access path is used and only those records meeting the
select/omit values are returned to the program. The processing is done as if the
DYNSLT keyword was specified for the file.)
Note: You cannot ignore the keyed sequence access path for logical file members
that are based on more than one physical file member.
Note: End of file delay should not be used for files that reuse deleted records.
If the file that is opened contains records that are longer than the length specified
in the program, the system allocates a storage area to match the file member’s
record length and this option is ignored. In this case, the entire record is passed to
the program. (However, some high-level languages allow you to access only that
portion of the record defined by the record length specified in the program.) If the
file that is opened contains records that are less than the length specified in the
program, the system allocates a storage area for the program-specified record
length. The program can use the extra storage space, but only the record lengths
defined for the file member are used for input/output operations.
For input operations (reads), the keyed sequence access path used is the one that
the file is opened with. Any other keyed sequence access paths that can exist over
the physical file are not considered. Also, any records in the keyed sequence access
path omitted because of select/omit specifications are not considered when
deciding if the key operation is a duplicate.
For output (write) and update operations, all nonunique keyed sequence access
paths of *IMMED maintenance that exist over the physical file are searched to
determine if the key for this output or update operation is a duplicate. Only keyed
sequence access paths that have *RBLD and *DLY maintenance are considered if
the access paths are actively open over the file at feedback time.
When you process a keyed file with a COBOL program, you can specify duplicate
key feedback to be returned to your program through the COBOL language, or on
the Open Database File (OPNDBF) or Open Query File (OPNQRYF) commands.
However, in COBOL having duplicate key feedback returned can cause a decline in
performance.
| You can lock a row in Operations Navigator by opening a table and editing the
| row you want to lock. Or, you can use the following operations to lock files,
| members, or data records:
| v “Locking Records”
| v “Locking Files” on page 108
| v “Locking Members” on page 108
| v “Locking Record Format Data” on page 108
For a list of commonly used database functions and the types of locks they place
on database files, see Appendix C. Database Lock Considerations.
Locking Records
WAITRCD Parameter. The AS/400 database has built-in integrity for records. For
example, if PGMA reads a record for update, it locks that record. Another program
may not read the same record for update until PGMA releases the record, but
another program could read the record just for inquiry. In this way, the system
ensures the integrity of the database.
The system determines the lock condition based on the type of file processing
specified in your program and the operation requested. For example, if your open
options include update or delete, each record read is locked so that any number of
users can read the record at the same time, but only one user can update the
record.
The system normally waits a specific number of seconds for a locked record to be
released before it sends your program a message that it cannot get the record you
are requesting. The default record wait time is 60 seconds; however, you can set
your own wait time through the WAITRCD parameter on the create and change
file commands and the override database file command. If your program is
notified that the record it wants is locked by another operation, you can have your
program take the appropriate action (for example, you could send a message to the
operator that the requested record is currently unavailable).
The system automatically releases a lock when the locked record is updated or
deleted. However, you can release record locks without updating the record. For
information on how to release a record lock, see your high-level language guide.
Note: Using commitment control changes the record locking rules. See the Backup
and Recovery book for more information on commitment control and its
effect on the record locking rules.
You can use the Display Record Locks (DSPRCDLCK) command to display the
current lock status (wait or held) of records for a physical file member. The
command will also indicate what type of lock is currently held. (For more
information about lock types, see the Backup and Recovery book.) Depending on the
parameters you specify, this command displays the lock status for a specific record
You can determine if your job currently has any records locked using the Check
Record Lock (CHKRCDLCK) command. This command returns a message (which
you can monitor) if your job has any locked records. The command is useful if you
are using group jobs. For example, you could check to see if you had any records
locked, before transferring to another group job. If you determined you did have
records locked, your program could release those locks.
Locking Files
WAITFILE Parameter. Some file operations exclusively allocate the file for the
length of the operation. During the time the file is allocated exclusively, any
program trying to open that file has to wait until the file is released. You can
control the amount of time a program waits for the file to become available by
specifying a wait time on the WAITFILE parameter of the create and change file
commands and the override database file command. If you do not specifically
request a wait time, the system defaults the file wait time to zero seconds.
A file is exclusively allocated when an operation that changes its attributes is run.
These operations (such as move, rename, grant or revoke authority, change owner,
or delete) cannot be run at the same time with any other operation on the same file
or on members of that file. Other file operations (such as display, open, dump, or
check object) only use the file definition, and thus lock the file less exclusively.
They can run at the same time with each other and with input/output operations
on a member.
Locking Members
Member operations (such as add and remove) automatically allocate the file
exclusively enough to prevent other file operations from occurring at the same
time. Input/output operations on the same member cannot be run, but
input/output operations on other members of the same file can run at the same
time.
Using the SHARE(*YES) parameter lets two or more programs running in the same
job or activation group share an open data path (ODP). An open data path is the
path through which all input/output operations for the file are performed. In a
sense, it connects the program to a file. If you do not specify the SHARE(*YES)
parameter, a new open data path is created every time a file is opened. If an active
file is opened more than once in the same job or activation group, you can use the
active ODP for the file with the current open of the file. You do not have to create
a new open data path.
This reduces the amount of time required to open the file after the first open, and
the amount of main storage required by the job or activation group. SHARE(*YES)
must be specified for the first open and other opens of the same file for the open
data path to be shared. A well-designed (for performance) application normally
shares an open data path with files that are opened in multiple programs in the
same job or activation group.
Specifying SHARE(*NO) tells the system not to share the open data path for a file.
Normally, this is specified only for those files that are seldom used or require
unique processing in specific programs.
The CPF4123 diagnostic message lists the mismatches that can be encountered
between the full open and the subsequent shared opens. These mismatches do not
cause the shared open to fail.
Note: The Open Query File (OPNQRYF) command never shares an existing shared
open data path in the job or activation group. If a shared ODP already exists
in the job or activation group with the same file, library, and member name
as the one specified on the Open Query File command, the system sends an
error message and the query file is not opened.
Example 1: Using a single set of files with similar processing options: In this
example, the user signs on and most of the programs used process the same set of
files.
A CL program (PGMA) is used as the first program (to set up the application,
including overrides and opening the shared files). PGMA then transfers control to
PGMB, which displays the application menu. Assume, in this example, that files A,
B, and C are used, and files A and B are to be shared. Files A and B were created
with SHARE(*NO); therefore an OVRDBF command should precede each of the
OPNDBF commands to specify the SHARE(*YES) option. File C was created with
SHARE(*NO) and File C is not to be shared in this example.
PGMA: PGM /* PGMA - Initial program */
OVRDBF FILE(A) SHARE(*YES)
OVRDBF FILE(B) SHARE(*YES)
OPNDBF FILE(A) OPTION(*ALL) ....
OPNDBF FILE(B) OPTION(*INP) ...
TFRCTL PGMB
ENDPGM
The files opened in PGMA are either scoped to the job, or PGMA, PGM11, and
PGM12 run in the same activation group and the file opens are scoped to that
activation group.
In this example, the Close File (CLOF) was not used because only one set of files is
required. When the operator signs off, the files are automatically closed. It is
assumed that PGMA (the initial program) is called only at the start of the job. For
information on how to reclaim resources in the Integrated Language Environment,
see the ILE Concepts book.
Any overrides and opens can be specified in the initial program (PGMA); then,
that program can be removed from the job (for example, by transferring out of it).
However, the open data paths that the program created when it opened the files
remain in existence and can be used by other programs in the job.
Example 2: Using multiple sets of files with similar processing options: Assume
that a menu requests the operator to specify the application program (for example,
accounts receivable or accounts payable) that uses the Open Database File
(OPNDBF) command to open the required files. When the application is ended, the
Close File (CLOF) command closes the files. The CLOF command is used to help
reduce the amount of main storage needed by the job. In this example, different
files are used for each application. The user normally works with one application
for a considerable length of time before selecting a new application.
The program for the accounts payable menu would be similar, but with a different
set of OPNDBF and CLOF commands.
For this example, files A and B were created with SHARE(*NO). Therefore, an
OVRDBF command must precede the OPNDBF command. As in Example 1, the
amount of main storage used by each job could be reduced by placing the
OPNDBF commands in a separate program and calling it. A separate program
could also be created for the CLOF commands. The OPNDBF commands could be
placed in an application setup program that is called from the menu, which
transfers control to the specific application program menu (any overrides specified
in this setup program are kept). However, calling separate programs for these
functions also uses system resources and, depending on the frequency with which
the different menus are used, it can be better to include the OPNDBF and CLOF
commands in each application program menu as shown in this example.
Another choice is to use the Reclaim Resources (RCLRSC) command in PGMC (the
setup program) instead of using the Close File (CLOF) commands. The RCLRSC
command closes any files and frees any leftover storage associated with any files
and programs that were called and have since returned to the calling program.
However, RCLRSC does not close files that are opened with the following specified
on the Open Database File (OPNDBF) or Open Query File (OPNQRYF) commands:
v OPNSCOPE(*ACTGRPDFN), and the open is requested from some activation
group other than the default.
v OPNSCOPE(*ACTGRP) reclaims if the RCLRSC command is from an activation
group with an activation group number that is lower than the activation group
number of the open.
v OPNSCOPE(*JOB).
v TYPE(*PERM).
The following example shows the RCLRSC command used to close files:
.
.
IF (&RESPONSE *EQ '1') DO
CALL ACCRECV
RCLRSC
ENDDO
IF (&RESPONSE *EQ '2') DO
CALL ACCPAY
RCLRSC
ENDDO
.
.
However, some options specified on the OPNDBF command can affect how the
program operates. For example, SEQONLY(*NO) is specified on the open
command for a file in the program. An error would occur if the OPNDBF
command used SEQONLY(*YES) and a program attempted an operation that was
not valid with sequential-only processing.
The ACCPTH parameter must also be consistent with the way programs will use
the access path (arrival or keyed).
Two OPNDBF commands could be used for the same data (for example, one with
OPTION(*ALL) and the other specifying OPTION(*INP)). The second use must be
a logical file pointing to the same physical file(s). This logical file can then be
opened as SHARE(*YES) and multiple uses made of it during the same job.
Note: File positioning operations are not considered sequential read operations;
therefore, a high-level language program containing positioning operations
will not automatically request sequential-only processing. (The SETLL
operation in the RPG/400 language and the START operation in the
COBOL/400* language are examples of file positioning operations.) Even
though the high-level language program can not automatically request
sequential-only processing, you can request it using the SEQONLY
parameter on the OVRDBF command.
The system also provides you a way to control the number of records that are
moved as a unit between auxiliary storage and main storage. If you are reading
the data in the file in the same order as the data is physically stored, you can
improve the performance of your job using the NBRRCDS parameter on the
OVRDBF command.
Note: Sequential-only processing should not be used with a keyed sequence access
path file unless the physical data is in the same order as the access path.
SEQONLY(*YES) processing may cause poor application performance until
the physical data is reorganized into the access path’s order.
For more details about the OPNQRYF command, see “Using the Open Query File
(OPNQRYF) Command” on page 125.
Note: Changes made after records are read into the input buffer are not
reflected in the input buffer.
v For output, your program must move one record at a time to the output buffer.
When the output buffer is full, the system automatically adds the records to the
database.
Note: If you are using a journal, the entire buffer is written to the journal at one
time as if the entries had logically occurred together. This journal
processing occurs before the records are added to the database.
If you use sequential-only processing for output, you might not see all the
changes made to the file as they occur. For example, if sequential-only is
specified for a file being used by PGMA, and PGMA is adding new records to
the file and the SEQONLY parameter was specified with 5 as the number of
records in the buffer, then only when the buffer is filled will the newly added
records be transferred to the database. In this example, only when the fifth
record was added, would the first five records be transferred to the database,
and be available for processing by other jobs in the system.
If multiple programs in the same job are sharing a sequential-only output file, the
output buffer is not emptied until the final close occurs. Consequently, a close
(other than the last close in the job) does not cause the records still in the buffer to
appear in the database for this or any other job.
Note: Any override parameters other than TOFILE, MBR, LVLCHK, SEQONLY,
SHARE, WAITRCD, and INHWRT are ignored by the OPNQRYF command.
For more information on the paging option see the Work Management book.
If you specify a member name, files that have the correct file name but do not
contain the member name are ignored. If you have multiple database files named
FILEA in different libraries, the member that is opened is the first one in the
library list that matches the request. For example, LIB1, LIB2, and LIB3 are in your
library list and all three contain a file named FILEA. Only FILEA in LIB3 has a
member named MBRA that is to be opened. Member MRBA in FILEA in LIB3 is
opened; the other FILEAs are ignored.
After finding the member, the system connects your program to the database file.
This allows your program to perform input/output operations to the file. For more
information about opening files in your high-level language program, see the
appropriate high-level language guide.
You can open a database file with statements in your high-level language program.
You can also use the CL open commands: Open Database File (OPNDBF) and
Open Query File (OPNQRYF). The OPNDBF command is useful in an initial
program in a job for opening shared files. The OPNQRYF command is very
effective in selecting and arranging records outside of your program. Then, your
program can use the information supplied by the OPNQRYF command to process
only the data it needs.
OPTION Parameter. Specify the *INP option if your application programs uses
input-only processing (reading records without updating records). This allows the
system to read records without trying to lock each one for possible update. Specify
the *OUT option if your application programs uses output-only processing (writing
records into a file but not reading or updating existing records).
Note: If your program does direct output operations to active records (updating by
relative record number), *ALL must be specified instead of *OUT. If your
program does direct output operations to deleted records only, *OUT must
be specified.
Note: You must specify a member name on the OVRDBF command to use a
member (other than the first member) to open in subsequent programs.
OPNID Parameter. If an identifier other than the file name is to be used, you must
specify it. The open identifier can be used in other CL commands to process the
file. For example, the Close File (CLOF) command uses the identifier to specify
which file is to be closed.
ACCPTH Parameter. If the file has a keyed sequence access path and either (1) the
open option is *OUT, or (2) the open option is *INP or *ALL, but your program
does not use the keyed sequence access path, then you can specify
ACCPTH(*ARRIVAL) on the OPNDBF parameter. Ignoring the keyed sequence
access path can improve your job’s performance.
OPNSCOPE Parameter. Specifies the scoping of the open data path (ODP). Specify
*ACTGRPDFN if the request is from the default activation group, and the ODP is
to be scoped to the call level of the program issuing the command. If the request is
from any other activation group, the ODP is scoped to that activation group.
Specify *ACTGRP if the ODP is to be scoped to the activation group of the
program issuing the command. Specify *JOB if the ODP is to be scoped to the job.
If you specify this parameter and the TYPE parameter you get an error message.
TYPE Parameter. Specify what you wish to happen when exceptions that are not
monitored occur in your application program. If you specify *NORMAL one of the
following can happen:
v Your program can issue a Reclaim Resources (RCLRSC) command to close the
files opened at a higher level in the call stack than the program issuing the
RCLRSC command.
v The high-level language you are using can perform a close operation.
Unlike a database file created with the Create Physical File (CRTPF) command or
the Create Logical File (CRTLF) command, the OPNQRYF command creates only a
temporary file for processing the data, it does not create a permanent file.
The OPNQRYF command has functions similar to those in DDS, and the CRTPF
and CRTLF commands. DDS requires source statements and a separate step to
create the file. OPNQRYF allows a dynamic definition without using DDS. The
OPNQRYF command does not support all of the DDS functions, but it supports
significant functions that go beyond the capabilities of DDS.
The OPNQRYF command parameters also have many functions similar to the SQL
SELECT statements. For example, the FILE parameter is similar to the SQL FROM
statement, the QRYSLT parameter is similar to the SQL WHERE statement, the
GRPFLD parameter is similar to the SQL GROUP BY statement, and the GRPSLT
parameter is similar to the SQL HAVING statement. For more information about
SQL, see DB2 UDB for AS/400 SQL Programming Concepts.
The following is a list of the major functions supplied by OPNQRYF. Each of these
functions is described later in this section.
v Dynamic record selection
v Dynamic keyed sequence access path
v Dynamic keyed sequence access path over a join
v Dynamic join
v Handling missing records in secondary join files
v Unique-key processing
v Mapped field definitions
v Group processing
v Final total-only processing
v Improving performance
v Open Query Identifier (ID)
To understand the OPNQRYF command, you must be familiar with its two
processing approaches: using a format in the file, and using a file with a different
format. The typical use of the OPNQRYF command is to select, arrange, and
format the data so it can be read sequentially by your high-level language
program.
See the CL Reference for OPNQRYF command syntax and parameter descriptions.
When you code your high-level language program, specify the name of the file
with the different format so the externally described field definitions of both
existing and derived fields can be processed by the program.
Before calling your high-level language program, you must specify an Override
with Database File (OVRDBF) command to direct your program file name to the
open query file. On the OPNQRYF command, specify both the database file and
the new file with the special format to be used by your high-level language
program. If the file you are querying does not have SHARE(*YES) specified, you
1 Specify the DDS for the file with the different record format, and create the
file. This file contains the fields that you want to process with your
high-level language program. Normally, data is not contained in this file,
and it does not require a member. You normally create this file as a
physical file without keys. A field reference file can be used to describe the
fields. The record format name can be different from the record format
name in the database file that is specified. You can use any database or
DDM file for this function. The file could be a logical file and it could be
indexed. It could have one or more members, with or without data.
2 Create the high-level language program to process the file with the record
format that you created in step 1. In this program, do not name the
database file that contains the data.
3 Run the Override with Database File (OVRDBF) command. Specify the
name of the file with the different (new) record format on the FILE
parameter. Specify the name of the database file that you want to query on
the TOFILE parameter. You can also specify a member name on the MBR
parameter. If the database member you are querying does not have
SHARE(*YES) specified, you must also specify SHARE(*YES) on the
OVRDBF command.
4 Run the Open Query File (OPNQRYF) command. Specify the database file
to be queried on the FILE parameter, and specify the name of the file with
the different (new) format that was created in step 1 on the FORMAT
parameter. Mapped field definitions can be required on the OPNQRYF
The previous steps show the normal flow using externally described data. It is not
necessary to create unique DDS and record formats for each OPNQRYF command.
You can reuse an existing record format. However, all fields in the record format
must be actual fields in the real database file or defined by mapped field
definitions. If you use program-described data, you can create the program at any
time.
You can use the file created in step 1 to hold the data created by the Open Query
File (OPNQRYF) command. For example, you can replace step 5 with a high-level
language processing program that copies data to the file with the different format,
or you may use the Copy from Query File (CPYFRMQRYF) command. The Copy
File (CPYF) command cannot be used. You can then follow step 5 with the CPYF
command or Query.
OPNQRYF Examples
The following sections describe how to specify both the OPNQRYF parameters for
each of the major functions discussed earlier and how to use the Open Query File
command with your high-level language program.
Notes:
1. If you run the OPNQRYF command from a command entry line with the
OPNSCOPE(*ACTGRPDFN) or TYPE(*NORMAL) parameter option, error
messages that occur after the OPNQRYF command successfully runs will not
close the file. Such messages would have closed the file prior to Version 2
Release 3 when TYPE(*NORMAL) was used. The system automatically runs the
Reclaim Resources (RCLRSC) command if an error message occurs, except for
message CPF0001, which is sent when the system detects an error in the
command. However, the RCLRSC command only closes files opened from the
default activation group at a higher level in the call stack than the level at
which the RCLRSC command was run.
2. After running a program that uses the Open Query File command for
sequential processing, the file position is normally at the end of the file. If you
want to run the same program or a different program with the same files, you
must position the file or close the file and open it with the same OPNQRYF
command. You can position the file with the Position Database File (POSDBF)
command. In some cases, a high-level language program statement can be
used.
In the following example, the fields INVCUS and INVPRD are defined as character
data:
QRYSLT('INVCUS *EQ "' *CAT &K1CUST *CAT '" *AND +
INVPRD *GE "' *CAT &LPRD *CAT '" *AND +
INVPRD *LE "' *CAT &HPRD *CAT '"')
If the fields were defined numeric data, the QRYSLT parameter could look like the
following:
QRYSLT('INVCUS *EQ ' *CAT &K1CUST *CAT ' *AND +
INVPRD *GE ' *CAT &LPRD *CAT ' *AND +
INVPRD *LE ' *CAT &HPRD *CAT ' ')
Zero length literal support changes the results of a comparison when used as the
compare argument of the contains (*CT) function. Consider the statement:
QRYSLT('field *CT ""')
With zero length literal support, the statement returns records that contain
anything. It is, in essence, a wildcard comparison for any number of characters
followed by any number of characters. It is equivalent to:
'field = %WLDCRD("**")'
Before zero length literal support, (before Version 2, Release 1, Modification 1), the
argument (″″) was interpreted as a single-byte blank. The statement returned
records that contained a single blank somewhere in the field. It was, in essence, a
wildcard comparison for any number of characters, followed by a blank, followed
by any number of characters. It was equivalent to:
'field = %WLDCRD("* *")'
See the OPNQRYF command in the CL Reference for a complete description of the
format of expressions used with the QRYSLT parameter.
Assume you want to select all the records from FILEA where the value of the Code
field is D. Your processing program is PGMB. PGMB only sees the records that
meet the selection value (you do not have to test in your program).
Note: You can specify parameters easier by using the prompt function for the
OPNQRYF command. For example, you can specify an expression for the
QRYSLT parameter without the surrounding delimiters because the system
will add the apostrophes.
Notice that numeric constants are not enclosed by two apostrophes (quotation
marks).
5. When comparing a field value to a CL variable, use apostrophes as follows
(only character CL variables can be used):
v If doing selection against a character, date, time, or timestamp field, specify:
OPNQRYF FILE(FILEA) QRYSLT('"' *CAT &CHAR *CAT '" *EQ FIELDA')
Notice that apostrophes and quotation marks enclose the CL variables and
*CAT operators.
When comparing two fields or constants, the data types must be compatible. The
following table describes the valid comparisons.
Table 7. Valid Data Type Comparisons for the OPNQRYF Command
Any Numeric Character Date1 Time1 Timestamp1
Any Numeric Valid Not Valid Not Valid Not Valid Not Valid
Character Not Valid Valid Valid2 Valid2 Valid2
Date1 Not Valid Valid2 Valid Not Valid Not Valid
1
Time Not Valid Valid2 Not Valid Valid Not Valid
Timestamp1 Not Valid Valid2 Not Valid Not Valid Valid
:
1
Date, time, and timestamp data types can be represented by fields and expressions,
but not constants; however, character constants can represent date, time, or
timestamp values.
2
The character field or constant must represent a valid date value if compared to a
date data type, a valid time value if compared to a time data type, or a valid
timestamp value if compared to a timestamp data type.
Note: For DBCS information, see Appendix B. Double-Byte Character Set (DBCS)
Considerations.
The performance of record selection can be greatly enhanced if some file on the
system uses the field being selected in a keyed sequence access path. This allows
the system to quickly access only the records that meet the selection values. If no
such access path exists, the system must read every record to determine if it meets
the selection values.
Even if an access path exists on the field you want to select from, the system may
not use the access path. For example, if it is faster for the system to process the
data in arrival sequence, it will do so. See the discussion in “Open Query File
command: Performance Considerations” on page 166 for more details.
Assume you want to process all records in which the Date field in the record is the
same as the current date. Also assume the Date field is in the same format as the
system date. In a CL program, you can specify:
DCL VAR(&CURDAT); TYPE(*CHAR) LEN(6)
RTVSYSVAL SYSVAL(QDATE) RTNVAR(&CURDAT);
OVRDBF FILE(FILEA) SHARE(*YES)
OPNQRYF FILE(FILEA) QRYSLT('"' *CAT &CURDAT *CAT '" *EQ DATE')
CALL PGM(PGMB)
CLOF OPNID(FILEA)
DLTOVR FILE(FILEA)
If the DATE field is defined as date data type, the preceding example could be
specified as:
OVRDBF FILE(FILEA) SHARE(*YES)
OPNQRYF FILE(FILEA) QRYSLT('%CURDATE *EQ DATE')
CALL PGM(PGMB)
CLOF OPENID(FILEA)
DLTOVR FILE(FILEA)
Note: The date field does not have to have the same format as the system date.
You could also specify the example as:
DCL VAR(&CVTDAT); TYPE(*CHAR) LEN(6)
DCL VAR(&CURDAT); TYPE(*CHAR) LEN(8)
RTVSYSVAL SYSVAL(QDATE) RTNVAR(&CVTDAT);
CVTDAT DATE(&CVTDAT); TOVAR(&CURDAT); TOSEP(/)
OVRDBF FILE(FILEA) SHARE(*YES)
OPNQRYF FILE(FILEA)
QRYSLT('"' *CAT &CURDAT *CAT '" *EQ DATE')
CALL PGM(PGMB)
CLOF OPNID (FILEA)
DLTOVR FILE(FILEA)
This is where DATE has a date data type in FILEA, the job default date format is
MMDDYY, and the job default date separator is the slash (/).
Note: For any character representation of a date in one of the following formats,
MMDDYY, DDMMYY, YYMMDD, or Julian, the job default date format and
separator must be the same to be recognized.
If, instead, you were using a constant, the QRYSLT would be specified as follows:
QRYSLT('"12/31/87" *EQ DATE')
The job default date format must be MMDDYY and the job default separator must
be the slash (/).
If a numeric field exists in the database and you want to compare it to a variable,
only a character variable can be used. For example, to select all records where a
packed Date field is greater than a variable, you must ensure the variable is in
character form. Normally, this will mean that before the Open Query File
(OPNQRYF) command, you use the Change Variable (CHGVAR) command to
change the variable from a decimal field to a character field. The CHGVAR
command would be specified as follows:
CHGVAR VAR(&CHARVAR); VALUE('123188')
The QRYSLT parameter would be specified as follows (see the difference from the
preceding examples):
QRYSLT(&CHARVAR *CAT ' *GT DATE')
If, instead, you were using a constant, the QRYSLT statement would be specified as
follows:
Assume you have a Date field specified in the character format YYMMDD and
with the “.” separator, and you want to process all records for 1988. You can
specify:
OVRDBF FILE(FILEA) SHARE(*YES)
OPNQRYF FILE(FILEA) QRYSLT('DATE *EQ %RANGE("88.01.01" +
"88.12.31") ')
CALL PGM(PGMC)
CLOF OPNID(FILEA)
DLTOVR FILE(FILEA)
This example would also work if the DATE field has a date data type, the job
default date format is YYMMDD, and the job default date separator is the period
(.).
Note: For any character representation of a date in one of the following formats,
MMDDYY, DDMMYY, YYMMDD, or Julian, the job default date format and
separator must be the same to be recognized.
If the ranges are variables defined as character data types, and the DATE field is
defined as a character data type, specify the QRYSLT parameter as follows:
QRYSLT('DATE *EQ %RANGE("' *CAT &LORNG *CAT '"' *BCAT '"' +
*CAT &HIRNG *CAT '")')
However, if the DATE field is defined as a numeric data type, specify the QRYSLT
parameter as follows:
QRYSLT('DATE *EQ %RANGE(' *CAT &LORNG *BCAT &HIRNG *CAT ')')
Assume you want to process all records in which the Addr field contains the street
named BROADWAY. The contains (*CT) function determines if the characters
appear anywhere in the named field. You can specify:
OVRDBF FILE(FILEA) SHARE(*YES)
OPNQRYF FILE(FILEA) QRYSLT('ADDR *CT "BROADWAY" ')
CALL PGM(PGMC)
CLOF OPNID(FILEA)
DLTOVR FILE(FILEA)
In this example, assume that the data is in uppercase in the database record. If the
data was in lowercase or mixed case, you could specify a translation function to
translate the lowercase or mixed case data to uppercase before the comparison is
made. The system-provided table QSYSTRNTBL translates the letters a through z
to uppercase. (You could use any translation table to perform the translation.)
Therefore, you can specify:
When the %XLATE function is used on the QRYSLT statement, the value of the
field passed to the high-level language program appears as it is in the database.
You can force the field to appear in uppercase using the %XLATE function on the
MAPFLD parameter.
Assume you want to process all records in which either the Amt field is equal to
zero, or the Lstdat field (YYMMDD order in character format) is equal to or less
than 88-12-31. You can specify:
OVRDBF FILE(FILEA) SHARE(*YES)
OPNQRYF FILE(FILEA) QRYSLT('AMT *EQ 0 *OR LSTDAT +
*LE "88-12-31" ')
CALL PGM(PGMC)
CLOF OPNID(FILEA)
DLTOVR FILE(FILEA)
This example would also work if the LSTDAT field has a date data type. The
LSTDAT field may be in any valid date format; however, the job default date
format must be YYMMDD and the job default date separator must be the dash (–).
Note: For any character representation of a date in one of the following formats,
MMDDYY, DDMMYY, YYMMDD, or Julian, the job default date format and
separator must be the same to be recognized.
Note that the &VARAMT variable must be defined as a character type. If the
variable is passed to your CL program as a numeric type, you must convert it to a
character type to allow concatenation. You can use the Change Variable (CHGVAR)
command to do this conversion.
Example 6: Selecting Records Using the OPNQRYF Command: Using the Open
Query File (OPNQRYF) command many times in a program
You can use the OPNQRYF command more than once in a high-level language
program. For example, assume you want to prompt the user for some selection
values, then display one or more pages of records. At the end of the first request
for records, the user may want to specify other selection values and display those
records. This can be done by doing the following:
1. Before calling the high-level language program, use an Override with Database
File (OVRDBF) command to specify SHARE(*YES).
2. In the high-level language program, prompt the user for the selection values.
When the program completes, run the Close File (CLOF) command or the Reclaim
Resources (RCLRSC) command to close the file, then delete the Override with
Database File command specified in step 1.
Note: An override command in a called CL program does not affect the open in
the main program. All overrides are implicitly deleted when the program is
ended. (However, you can use a call to program QCMDEXC from your
high-level language program to specify an override, if needed.)
Assume you have a packed decimal Date field in the format MMDDYY and you
want to select all the records for the year 1988. You cannot select records directly
from a portion of a packed decimal field, but you can use the MAPFLD parameter
on the OPNQRYF command to create a new field that you can then use for
selecting part of the field.
where:
In this example, if DATE was a date data type, it could be specified as follows:
The first mapped field definition specifies that the Char6 field be created from the
packed decimal Date field. The %DIGITS function converts from packed decimal to
character and ignores any decimal definitions (that is, 1234.56 is converted to
’123456’). Because no definition of the Char6 field is specified, the system assigns a
length of 6. The second mapped field defines the Year field as type *CHAR
(character) and length 2. The expression uses the substring function to map the last
2 characters of the Char6 field into the Year field.
Note that the mapped field definitions are processed in the order in which they are
specified. In this example, the Date field was converted to character and assigned
to the Char6 field. Then, the last two digits of the Char6 field (the year) were
assigned to the Year field. Any changes to this order would have produced an
incorrect result.
Note: Mapped field definitions are always processed before the QRYSLT parameter
is evaluated.
You could accomplish the same result by specifying the substring on the QRYSLT
parameter and dropping one of the mapped field definitions as follows:
OPNQRYF FILE(FILEA) +
QRYSLT('%SST(CHAR6 5 2) *EQ "88" ') +
MAPFLD((CHAR6 '%DIGITS(DATE)'))
Assume you have a packed decimal Date field in the format MMDDYY and you
want to select the records for March 1988. To do this, you can specify:
OVRDBF FILE(FILEA) SHARE(*YES)
OPNQRYF FILE(FILEA) +
QRYSLT('%DIGITS(DATE) *EQ %WLDCRD("03__88")')
CALL PGM(PGMC)
CLOF OPNID(FILEA)
DLTOVR FILE(FILEA)
Note that the only time the MAPFLD parameter is needed to define a database
field for the result of the %DIGITS function is when the result needs to be used
with a function that only supports a simple field name (not a function or
expression) as an argument. The %WLDCRD operation has no such restriction on
the operand that appears before the *EQ operator.
Note that although the field in the database is in numeric form, double
apostrophes surround the literal to make its definition the same as the Char6 field.
The wildcard function is not supported for DATE, TIME, or TIMESTAMP data
types.
The %WLDCRD function lets you select any records that match your selection
values, in which the underline (_) will match any single character value. The two
underline characters in Example 8 allow any day in the month of March to be
selected. The %WLDCRD function also allows you to name the wild card character
(underline is the default).
This compares successfully to ABC, ACC, ADC, AxC, and so on. In this example,
the field being analyzed only compares correctly if it is exactly 3 characters in
length. If the field is longer than 3 characters, you also need the second form of
wild card support.
v A variable-position wild card will match any zero or more characters. The Open
Query File (OPNQRYF) command uses an asterisk (*) for this type of wild card
variable character or you can specify your own character. An asterisk is used in
the following example:
QRYSLT('FLDB *EQ %WLDCRD("A*C*") ')
This compares successfully to AC, ABC, AxC, ABCD, AxxxxxxxC, and so on. The
asterisk causes the command to ignore any intervening characters if they exist.
Notice that in this example the asterisk is specified both before and after the
character or characters that can appear later in the field. If the asterisk were
omitted from the end of the search argument, it causes a selection only if the
field ends with the character C.
You must specify an asterisk at the start of the wild card string if you want to
select records where the remainder of the pattern starts anywhere in the field.
Similarly, the pattern string must end with an asterisk if you want to select
records where the remainder of the pattern ends anywhere in the field.
You can combine the two wildcard functions as in the following example:
QRYSLT('FLDB *EQ %WLDCRD("ABC_*DEF*") ')
You would not select the other records because a comparison is made with blanks
added to the value you specified. The way to select all four names is to specify:
QRYSLT('NAME *EQ %WLDCRD("JOHNS*")')
Note: For information about using the %WLDCRD function for DBCS, see
Appendix B. Double-Byte Character Set (DBCS) Considerations.
Complex selection statements can also be specified. For example, you can specify:
QRYSLT('DATE *EQ "880101" *AND AMT *GT 5000.00')
The rules governing the priority of processing the operators are described in the
CL Reference. Some of the rules are:
v The *AND operations are processed first; therefore, the record would be selected
if:
You can also use the symbols described in the CL Referenceinstead of the
abbreviated form (for example, you can use = instead of *EQ) as in the following
example:
QRYSLT('CODE = "A" & TYPE = "X" | AMT > 5000.00')
Example 10: Selecting Records Using the OPNQRYF Command: Using coded
character set identifiers (CCSIDs)
Each character and DBCS field in all database files is tagged with a CCSID. This
CCSID allows you to further define the data stored in the file so that any
comparison, join, or display of the fields is performed in a meaningful way. For
example, if you compared FIELD1 in FILE1 where FIELD1 has a CCSID of 37
(USA) to FIELD2 in FILE2 where FILED2 has a CCSID of 273 (Austria, Germany)
appropriate mapping would occur to make the comparison meaningful.
OPNQRYF FILE(FILEA FILEB) FORMAT(RESULTF) +
JFLD((FILEA/NAME FILEB/CUSTOMER))
If field NAME has a CCSID of 37 and field CUSTOMER has a CCSID of 273, the
mapping of either NAME or CUSTOMER is performed during processing of the
OPNQRYF command so that the join of the two fields provides a meaningful
result.
Normally, constants defined in the MAPFLD, QRYSLT, and GRPSLT parameters are
tagged with the CCSID defined to the current job. This suggests that when two
users with different job CCSIDs run the same OPNQRYF command (or a program
containing an OPNQRYF command) and the OPNQRYF has constants defined in
it, the users can get different results because the CCSID tagged to the constants
may cause the constants to be treated differently.
You can tag a constant with a specific CCSID by using the MAPFLD parameter. By
specifying a MAPFLD whose definition consists solely of a constant and then
specifying a CCSID for the MAPFLD the constant becomes tagged with the CCSID
specified in the MAPFLD parameter. For example:
OPNQRYF FILE(FILEA) FORMAT(RESULTF) QRYSLT('NAME *EQ MAP1') +
MAPFLD((MAP1 '"Smith"' *CHAR 5 *N 37))
The constant “Smith” is tagged with the CCSID 37 regardless of the job CCSID of
the user issuing the OPNQRYF command. In this example, all users would get the
same result records (although the result records would be mapped to the user’s job
CCSID). Conversely, if the query is specified as:
OPNQRYF FILE(FILEA) FORMAT(RESULTF) QRYSLT('NAME *EQ "Smith"')
the results of the query may differ, depending on the job CCSID of the user issuing
the OPNQRYF command.
Example 11: Selecting Records Using the OPNQRYF Command: Using Sort
Sequence and Language Identifier
To see how to use a sort sequence, run the examples in this section against the
STAFF file shown in Table 8.
Table 8. The STAFF File
ID NAME DEPT JOB YEARS SALARY COMM
10 Sanders 20 Mgr 7 18357.50 0
20 Pernal 20 Sales 8 18171.25 612.45
30 Merenghi 38 MGR 5 17506.75 0
40 OBrien 38 Sales 6 18006.00 846.55
50 Hanes 15 Mgr 10 20659.80 0
60 Quigley 38 SALES 00 16808.30 650.25
In the examples, the results are shown for a particular statement using each of the
following:
v *HEX sort sequence.
v Shared-weight sort sequence for language identifier ENU.
v Unique-weight sort sequence for language identifier ENU.
The following command selects records with the value MGR in the JOB field:
OPNQRYF FILE(STAFF) QRYSLT('JOB *EQ "MGR"')
Table 9 shows the record selection with the *HEX sort sequence. The records that
match the record selection criteria for the JOB field are selected exactly as specified
in the QRYSLT statement; only the uppercase MGR is selected.
Table 9. Using the *HEX Sort Sequence. OPNQRYF FILE(STAFF) QRYSLT(’JOB *EQ
″MGR″’) SRTSEQ(*HEX)
ID NAME DEPT JOB YEARS SALARY COMM
30 Merenghi 38 MGR 5 17506.75 0
Table 10 shows the record selection with the shared-weight sort sequence. The
records that match the record selection criteria for the JOB field are selected by
treating uppercase and lowercase letters the same. With this sort sequence, mgr,
Mgr, and MGR values are selected.
Table 10. Using the Shared-Weight Sort Sequence. OPNQRYF FILE(STAFF) QRYSLT(’JOB
*EQ ″MGR″’) SRTSEQ(LANGIDSHR) LANGID(ENU)
ID NAME DEPT JOB YEARS SALARY COMM
10 Sanders 20 Mgr 7 18357.50 0
30 Merenghi 38 MGR 5 17506.75 0
50 Hanes 15 Mgr 10 20659.80 0
100 Plotz 42 mgr 6 18352.80 0
Table 11 shows the record selection with the unique-weight sort sequence. The
records that match the record selection criteria for the JOB field are selected by
treating uppercase and lowercase letters as unique. With this sort sequence, the
mgr, Mgr, and MGR values are all different. The MGR value is selected.
Table 11. Using the Unique-Weight Sort Sequence. OPNQRYF FILE(STAFF) QRYSLT(’JOB
*EQ ″MGR″’) SRTSEQ(LANGIDUNQ) LANGID(ENU)
ID NAME DEPT JOB YEARS SALARY COMM
30 Merenghi 38 MGR 5 17506.75 0
Assume you want to process the records in FILEA arranged by the value in the
Cust field with program PGMD. You can specify:
OVRDBF FILE(FILEA) SHARE(*YES)
OPNQRYF FILE(FILEA) KEYFLD(CUST)
CALL PGM(PGMD)
CLOF OPNID(FILEA)
DLTOVR FILE(FILEA)
Note: The FORMAT parameter on the Open Query File (OPNQRYF) command is
not needed because PGMD is created by specifying FILEA as the processed
file. FILEA can be an arrival sequence or a keyed sequence file. If FILEA is
keyed, its key field can be the Cust field or a totally different field.
If you want the records to be processed by Cust sequence and then by Date in Cust,
specify:
OPNQRYF FILE(FILEA) KEYFLD(CUST DATE)
In these two examples, the FORMAT parameter is not used. (If a different format is
defined, all key fields must exist in the format.)
To process the records by the JOB field values with a unique-weight sort sequence
using the STAFF file in Table 8 on page 140, specify:
OPNQRYF FILE(STAFF) KEYFLD(JOB) SRTSEQ(*LANGIDUNQ) LANGID(ENU)
To process the records by the JOB field values with a unique-weight sort sequence
using the STAFF file in Table 8 on page 140, specify:
OPNQRYF FILE(STAFF) KEYFLD(JOB) SRTSEQ(*LANGIDSHR) LANGID(ENU)
The results from this query will be similar to the results in Example 3. The mgr
and sales entries could be in any sequence because the uppercase and lowercase
letters are treated as equals. That is, the shared-weight sort sequence treats mgr,
Mgr, and MGR as equal values. Likewise, sales, Sales, and SALES are treated as
equal values.
The specification is identical to the previous method. The access path is specified
using whatever key fields are required. There is no restriction on which physical
file the key fields are in. However, if a key field exists in other than the primary
file of a join specification, the system must make a temporary copy of the joined
records. The system must also build a keyed sequence access path over the copied
records before the query file is opened. The key fields must exist in the format
identified on the FORMAT parameter.
Assume you already have a join logical file named JOINLF. FILEX is specified as
the primary file and is joined to FILEY. You want to process the records in JOINLF
by the Descrp field which is in FILEY.
If you want to arrange the records by Qty in Descrp (Descrp is the primary key
field and Qty is a secondary key field) you can specify:
OPNQRYF FILE(JOINLF) KEYFLD(DESCRP QTY)
In the following examples, it is assumed that the file specified on the FORMAT
parameter was created. You will normally want to create the file before you create
the processing program so you can use the externally described data definitions.
The default for the join order (JORDER) parameter is used in all of the following
examples. The default for the JORDER parameter is *ANY, which tells the system
that it can determine the order in which to join the files. That is, the system
determines which file to use as the primary file and which as the secondary files.
This allows the system to try to improve the performance of the join function.
The join criterion, like the record selection criterion, is affected by the sort sequence
(SRTSEQ) and the language identifier (LANGID) specified (see “Example 11:
Selecting Records Using the OPNQRYF Command” on page 140).
Assume you want to join FILEA and FILEB. Assume the files contain the following
fields:
The join field is Cust which exists in both files. Any record format name can be
specified in the Open Query File (OPNQRYF) command for the join file. The file
does not need a member. The records are not required to be in keyed sequence.
File JOINAB is a physical file with no data. This is the file that contains the record
format to be specified on the FORMAT parameter in the Open Query File
(OPNQRYF) command.
Notice that the TOFILE parameter on the Override with Database File (OVRDBF)
command specifies the name of the primary file for the join operation (the first file
specified for the FILE parameter on the OPNQRYF command). In this example, the
FILE parameter on the Open Query File (OPNQRYF) command identifies the files
in the sequence they are to be joined (A to B). The format for the file is in the file
JOINAB.
The JFLD parameter identifies the Cust field in FILEA to join to the Cust field in
FILEB. Because the Cust field is not unique across all of the joined record formats,
it must be qualified on the JFLD parameter. The system attempts to determine, in
The system joins FILEA and FILEB using the Cust field because of the values
specified for the QRYSLT parameter. Notice that in this example the JFLD
parameter is not specified on the command. However, if either
JDFTVAL(*ONLYDFT) or JDFTVAL(*YES) is specified on the OPNQRYF command,
the JFLD parameter must be specified.
The MAPFLD parameter is needed on the Open Query File (OPNQRYF) command
to describe which file should be used for the data for the Cust field in the record
format for file JOINAB. If a field is defined on the MAPFLD parameter, its
unqualified name (the Cust field in this case without the file name identification)
can be used anywhere else in the OPNQRYF command. Because the Cust field is
defined on the MAPFLD parameter, the first value of the JFLD parameter need not
be qualified. For example, the same result could be achieved by specifying:
JFLD((CUST FILEB/CUST)) +
MAPFLD((CUST 'FILEA/CUST'))
Any other uses of the same field name in the Open Query File (OPNQRYF)
command to indicate a field from a file other than the file defined by the MAPFLD
parameter must be qualified with a file name.
The JDFTVAL parameter (similar to the JDFTVAL keyword in DDS) can also be
specified on the Open Query File (OPNQRYF) command to describe what the
system should do if one of the records is missing from the secondary file. In this
example, the JDFTVAL parameter was not specified, so only the records that exist
in both files are selected.
If you tell the system to improve the results of the query (through parameters on
the OPNQRYF command), it will generally try to use the file with the smallest
number of records selected as the primary file. However, the system will also try to
avoid building a temporary file.
You can force the system to follow the file sequence of the join as you have
specified it in the FILE parameter on the Open Query File (OPNQRYF) command
by specifying JORDER(*FILE). If JDFTVAL(*YES) or JDFTVAL(*ONLYDFT) is
specified, the system will never change the join file sequence because a different
sequence could cause different results.
Assume you want to join files FILEAB, FILECD, and FILEEF to select only those
records with matching records in secondary files. Define a file JOINF and describe
the format that should be used. Assume the record formats for the files contain the
In this case, all field names in the files that make up the join file begin with a
2-character prefix (identical for all fields in the file) and end with a suffix that is
identical across all the files (for example, xxitm). This makes all field names unique
and avoids having to qualify them.
The xxitm field allows the join from FILEAB to FILECD. The two fields xxitm and
xxcolr allow the join from FILECD to FILEEF. A keyed sequence access path does
not have to exist for these files. However, if a keyed sequence access path does
exist, performance may improve significantly because the system will attempt to
use the existing access path to arrange and select records, where it can. If access
paths do not exist, the system automatically creates and maintains them as long as
the file is open.
OVRDBF FILE(JOINF) TOFILE(FILEAB) SHARE(*YES)
OPNQRYF FILE(FILEAB FILECD FILEEF) +
FORMAT(JOINF) +
JFLD((ABITM CDITM)(CDITM EFITM) +
(CDCOLR EFCOLR))
CALL PGM(PGME) /* Created using file JOINF as input */
CLOF OPNID(FILEAB)
DLTOVR FILE(JOINF)
The join field pairs do not have to be specified in the order shown above. For
example, the same result is achieved with a JFLD parameter value of:
JFLD((CDCOLR EFCOLR)(ABITM CDITM) (CDITM EFITM))
The attributes of each pair of join fields do not have to be identical. Normal
padding of character fields and decimal alignment for numeric fields occurs
automatically.
The JDFTVAL parameter is not specified so *NO is assumed and no default values
are used to construct join records. If you specified JDFTVAL(*YES) and there is no
record in file FILECD that has the same join field value as a record in file FILEAB,
defaults are used for the Cddscp and Cdcolr fields to join to file FILEEF. Using these
defaults, a matching record can be found in file FILEEF (depending on if the
default value matches a record in the secondary file). If not, a default value
appears for these files and for the Efqty field.
You can use fields defined on the MAPFLD parameter for either one of the join
field pairs. This is useful when the key in the secondary file is defined as a single
field (for example, a 6-character date field) and there are separate fields for the
same information (for example, month, day, and year) in the primary file. Assume
FILEA has character fields Year, Month, and Day and needs to be joined to FILEB
which has the Date field in YYMMDD format. Assume you have defined file
JOINAB with the desired format. You can specify:
Because any values returned by the join operation for the fields in FILEB are
defaults, it is normal to use only the format for FILEA. The records that appear are
those that do not have a match in FILEB. The FORMAT parameter is required
whenever the FILE parameter describes more than a single file, but the file name
specified can be one of the files specified on the FILE parameter. The program is
created using FILEA.
Conversely, you can also get a list of all the records where there is a record in
FILEB that does not have a match in FILEA. You can do this by making the
secondary file the primary file in all the specifications. You would specify:
OVRDBF FILE(FILEB) SHARE(*YES)
OPNQRYF FILE(FILEB FILEA) FORMAT(FILEB) JFLD((CUST FILEA/CUST)) +
MAPFLD((CUST 'FILEB/CUST')) JDFTVAL(*ONLYDFT)
CALL PGM(PGMF) /* Created using file FILEB as input */
CLOF OPNID(FILEB)
DLTOVR FILE(FILEB)
Note: The Override with Database File (OVRDBF) command in this example uses
FILE(FILEB) because it must specify the first file on the OPNQRYF FILE
Unique-Key Processing
Unique-key processing allows you to process only the first record of a group. The
group is defined by one or more records with the same set of key values.
Processing the first record implies that the records you receive will have unique
keys.
When you use unique-key processing, you can only read the file sequentially. The
key fields are sorted according to the specified sort sequence (SRTSEQ) and
language identifier (LANGID) (see “Example 3: Specifying a Keyed Sequence
Access Path without Using DDS” on page 142 and “Example 4: Specifying a Keyed
Sequence Access Path without Using DDS” on page 143).
Assume you want to process FILEA, which has records with duplicate keys for the
Cust field. You want only the first record for each unique value of the Cust field to
be processed by program PGMF. You can specify:
OVRDBF FILE(FILEA) SHARE(*YES)
OPNQRYF FILE(FILEA) KEYFLD(CUST) UNIQUEKEY(*ALL)
CALL PGM(PGMF)
CLOF OPNID(FILEA)
DLTOVR FILE(FILEA)
Example 2: Unique-Key Processing: Reading records using only some of the key
fields
Assume you want to process the same file with the sequence: Slsman, Cust, Date,
but you want only one record per Slsman and Cust. Assume the records in the file
are:
You specify the number of key fields that are unique, starting with the first key
field.
OVRDBF FILE(FILEA) SHARE(*YES)
OPNQRYF FILE(FILEA) KEYFLD(SLSMAN CUST DATE) UNIQUEKEY(2)
CALL PGM(PGMD)
CLOF OPNID(FILEA)
DLTOVR FILE(FILEA)
Assume you have the Price and Qty fields in the record format. You can multiply
one field by the other by using the Open Query File (OPNQRYF) command to
create the derived Exten field. You want FILEA to be processed, and you have
already created FILEAA. Assume the record formats for the files contain the
following fields:
FILEA FILEAA
Order Order
Item Item
Qty Exten
Price Brfdsc
Descrp
The Exten field is a mapped field. Its value is determined by multiplying Qty times
Price. It is not necessary to have either the Qty or Price field in the new format, but
they can exist in that format, too if you wish. The Brfdsc field is a brief description
of the Descrp field (it uses the first 10 characters).
Assume you have specified PGMF to process the new format. To create this
program, use FILEAA as the file to read. You can specify:
OVRDBF FILE(FILEAA) TOFILE(FILEA) SHARE(*YES)
OPNQRYF FILE(FILEA) FORMAT(FILEAA) +
MAPFLD((EXTEN 'PRICE * QTY') +
(BRFDSC 'DESCRP'))
CALL PGM(PGMF) /* Created using file FILEAA as input */
CLOF OPNID(FILEA)
DLTOVR FILE(FILEAA)
Notice that the attributes of the Exten field are those defined in the record format
for FILEAA. If the value calculated for the field is too large, an exception is sent to
the program.
It is not necessary to use the substring function to map to the Brfdsc field if you
only want the characters from the beginning of the field. The length of the Brfdsc
field is defined in the FILEAA record format.
Notice quotation marks enclose a blank value. By using a constant for the
definition of an unused field, you avoid having to create a unique format for each
use of the OPNQRYF command.
Assume you want to calculate a mathematical function that is the sine of the Fldm
field in FILEA. First create a file (assume it is called FILEAA) with a record format
containing the following fields:
FILEA FILEAA
Code Code
Fldm Fldm
Sinm
You can then create a program (assume PGMF) using FILEAA as input and
specify:
OVRDBF FILE(FILEAA) TOFILE(FILEA) SHARE(*YES)
OPNQRYF FILE(FILEA) FORMAT(FILEAA) +
MAPFLD((SINM '%SIN(FLDM)'))
CALL PGM(PGMF) /* Created using file FILEAA as input */
CLOF OPNID(FILEA)
DLTOVR FILE(FILEAA)
The built-in function %SIN calculates the sine of the field specified as its argument.
Because the Sinm field is defined in the format specified on the FORMAT
parameter, the OPNQRYF command converts its internal definition of the sine
value (in floating point) to the definition of the Sinm field. This technique can be
used to avoid certain high-level language restrictions regarding the use of
floating-point fields. For example, if you defined the Sinm field as a packed
decimal field, PGMF could be written using any high-level language, even though
the value was built using a floating-point field.
There are many other functions besides sine that can be used. Refer to the
OPNQRYF command in the CL Reference for a complete list of built-in functions.
Assume, in the previous example, that a field called Fldx also exists in FILEA, and
the Fldx field has appropriate attributes used to hold the sine of the Fldm field.
Also assume that you are not using the contents of the Fldx field. You can use the
MAPFLD parameter to change the contents of a field before passing it to your
high-level language program. For example, you can specify:
In this case, you do not need to specify a different record format on the FORMAT
parameter. (The default uses the format of the first file on the FILE parameter.)
Therefore, the program is created by using FILEA. When using this technique, you
must ensure that the field you redefine has attributes that allow the calculated
value to process correctly. The least complicated approach is to create a separate
file with the specific fields you want to process for each query.
You can also use this technique with a mapped field definition and the %XLATE
function to translate a field so that it appears to the program in a different manner
than what exists in the database. For example, you can translate a lowercase field
so the program only sees uppercase.
The sort sequence and language identifier can affect the results of the %MIN and
%MAX built-in functions. For example, the uppercase and lowercase versions of
letters can be equal or unequal depending on the selected sort sequence and
language identifier. Note that the translated field value is used to determine the
minimum and maximum, but the untranslated value is returned in the result
record.
The example described uses FILEA as an input file. You can also update data using
the OPNQRYF command. However, if you use a mapped field definition to change
a field, updates to the field are ignored.
Record selection is normally done before field mapping errors occur (for example,
where field mapping would cause a division error). Therefore, a record can be
omitted (based on the QRYSLT parameter values and valid data in the record) that
would have caused a divide-by-zero error. In such an instance, the record would
be omitted and processing by the OPNQRYF command would continue.
If you want a zero answer, the following describes a solution that is practical for
typical commercial data.
The %MAX function returns the maximum value of either B * B or a small value.
The small value must have enough leading zeros so that it is less than any value
calculated by B * B unless B is zero. In this example, B has zero decimal positions
The intent of the first multiplication is to produce a zero dividend if B is zero. This
will ensure a zero result when the division occurs. Dividing by zero does not occur
if B is zero because the .1 value will be the value used as the divisor.
You normally start by creating a file with a record format containing only the
following types of fields:
v Grouping fields. Specified on the GRPFLD parameter that define groups. Each
group contains a constant set of values for all grouping fields. The grouping
fields do not need to appear in the record format identified on the FORMAT
parameter.
v Aggregate fields. Defined by using the MAPFLD parameter with one or more of
the following built-in functions:
%COUNT
Counts the records in a group
%SUM
A sum of the values of a field over the group
%AVG
Arithmetic average (mean) of a field, over the group
%MAX
Maximum value in the group for the field
%MIN
Minimum value in the group for the field
%STDDEV
Standard deviation of a field, over the group
%VAR Variance of a field, over the group
v Constant fields. Allow constants to be placed in field values. The restriction that
the Open Query File (OPNQRYF) command must know all fields in the output
format is also true for the grouping function.
When you use group processing, you can only read the file sequentially.
Assume you want to group the data by customer number and analyze the amount
field. Your database file is FILEA and you create a file named FILEAA containing a
record format with the following fields:
FILEA FILEAA
Cust Cust
Type Count (count of records per customer)
Amt Amtsum (summation of the amount field)
Amtavg (average of the amount field)
Amtmax (maximum value of the amount field)
When you define the fields in the new file, you must ensure that they are large
enough to hold the results. For example, if the Amt field is defined as 5 digits, you
may want to define the Amtsum field as 7 digits. Any arithmetic overflow causes
your program to end abnormally.
You then create a program (PGMG) using FILEAA as input to print the records.
OVRDBF FILE(FILEAA) TOFILE(FILEA) SHARE(*YES)
OPNQRYF FILE(FILEA) FORMAT(FILEAA) KEYFLD(CUST) +
GRPFLD(CUST) MAPFLD((COUNT '%COUNT') +
(AMTSUM '%SUM(AMT)') +
(AMTAVG '%AVG(AMT)') +
(AMTMAX '%MAX(AMT)'))
CALL PGM(PGMG) /* Created using file FILEAA as input */
CLOF OPNID(FILEA)
DLTOVR FILE(FILEAA)
Note: If you specify the GRPFLD parameter, the groups may not appear in
ascending sequence. To ensure a specific sequence, you should specify the
KEYFLD parameter.
Assume you want to print only the summary records in this example in which the
Amtsum value is greater than 700.00. Because the Amtsum field is an aggregate field
for a given customer, use the GRPSLT parameter to specify selection after
grouping. Add the GRPSLT parameter:
The Open Query File (OPNQRYF) command supports selection both before
grouping (QRYSLT parameter) and after grouping (GRPSLT parameter).
Assume you want to select additional customer records in which the Type field is
equal to A. Because Type is a field in the record format for file FILEA and not an
aggregate field, you add the QRYSLT statement to select before grouping as
follows:
QRYSLT('TYPE *EQ "A" ')
Note that fields used for selection do not have to appear in the format processed
by the program.
Notice the values for CUST 001 changed because the selection took place before
the grouping took place.
Assume you want to arrange the output by the Amtavg field in descending
sequence, in addition to the previous QRYSLT parameter value. You can do this by
changing the KEYFLD parameter on the OPNQRYF command as:
KEYFLD((AMTAVG *DESCEND))
Assume you have a database file FILEA and decide to create file FINTOT for your
final total record as follows:
FILEA FINTOT
Code Count (count of all the selected records)
Amt Totamt (total of the amount field)
Maxamt (maximum value in the amount field)
Assume you want to change the previous example so that only the records where
the Code field is equal to B are in the final total. You can add the QRYSLT
parameter as follows:
OVRDBF FILE(FINTOT) TOFILE(FILEA) SHARE(*YES)
OPNQRYF FILE(FILEA) FORMAT(FINTOT) +
QRYSLT('CODE *EQ "B" ') MAPFLD((COUNT '%COUNT') +
(TOTAMT '%SUM(AMT)') (MAXAMT '%MAX(AMT)'))
CALL PGM(PGMG) /* Created using file FINTOT as input */
CLOF OPNID(FILEA)
DLTOVR FILE(FINTOT)
You can use the GRPSLT keyword with the final total function. The GRPSLT
selection values you specify determines if you receive the final total record.
Assume you want to process the new file/format with a CL program. You want to
read the file and send a message with the final totals. You can specify:
DCLF FILE(FINTOT)
DCL &COUNTA *CHAR LEN(7)
DCL &TOTAMTA *CHAR LEN(9)
OVRDBF FILE(FINTOT) TOFILE(FILEA) SHARE(*YES)
OPNQRYF FILE(FILEA) FORMAT(FINTOT) MAPFLD((COUNT '%COUNT') +
(TOTAMT '%SUM(AMT)'))
RCVF
CLOF OPNID(FILEA)
CHGVAR &COUNTA &COUNT
CHGVAR &TOTAMTA &TOTAMT
SNDPGMMSG MSG('COUNT=' *CAT &COUNTA *CAT +
' Total amount=' *CAT &TOTAMTA);
DLTOVR FILE(FINTOT)
You must convert the numeric fields to character fields to include them in an
immediate message.
When you use the Open Query File (OPNQRYF) command there are two steps
where performance considerations exist. The first step is during the actual
processing of the OPNQRYF command itself. This step decides if OPNQRYF is
going to use an existing access path or build a new one for this query request. The
second step when performance considerations play a role is when the application
For most batch type functions, you are usually only interested in the total time of
both steps mentioned above. Therefore, the default for OPNQRYF is
OPTIMIZE(*ALLIO). This means that OPNQRYF will consider the total time it
takes for both steps.
If you want to process the same results of OPNQRYF with multiple programs, you
would want the first step to make an efficient open data path (ODP). That is, you
would try to minimize the number of records that must be read by the processing
program in the second step by specifying OPTIMIZE(*MINWAIT) on the
OPNQRYF command.
Example 1: Controlling How the System Runs the Open Query File Command:
Optimizing for the first set of records
Assume that you have an interactive job in which the operator requests all records
where the Code field is equal to B. Your program’s subfile contains 15 records per
screen. You want to get the first screen of results to the operator as quickly as
possible. You can specify:
OVRDBF FILE(FILEA) SHARE(*YES)
OPNQRYF FILE(FILEA) QRYSLT('CODE = "B" ') +
SEQONLY(*YES 15) OPTIMIZE(*FIRSTIO)
CALL PGM(PGMA)
CLOF OPNID(FILEA)
DLTOVR FILE(FILEA)
The system optimizes handling the query and fills the first buffer with records
before completing the entire query regardless of whether an access path already
exists over the Code field.
Example 2: Controlling How the System Runs the Open Query File Command:
Optimizing to minimize the number of records read
Assume that you have multiple programs that will access the same file which is
built by the Open Query File (OPNQRYF) command. In this case, you will want to
optimize the performance so that the application programs read only the data they
are interested in. This means that you want OPNQRYF to perform the selection as
efficiently as possible. You could specify:
OVRDBF FILE(FILEA) SHARE(*YES)
OPNQRYF FILE(FILEA) QRYSLT('CODE *EQ "B"') +
KEYFLD(CUST) OPTIMIZE(*MINWAIT)
CALL PGM(PGMA)
When you specify the KEYFLD(*FILE) parameter option for the Open Query File
(OPNQRYF) command, and a sort sequence other than *HEX has been specified for
the query with the job default or the OPNQRYF SRTSEQ parameter, you can
receive your records in an order that does not reflect the true file order. If the file is
keyed, the query’s sort sequence is applied to the key fields of the file and
informational message CPI431F is sent. The file’s sort sequence and alternative
collating sequence table are ignored for the ordering, if they exist. This allows
users to indicate which fields to apply a sort sequence to without having to list all
the field names. If a sort sequence is not specified for the query (for example,
*HEX), ordering is done as it was prior to Version 2 Release 3.
If you use the FORMAT parameter, your program specifies the same file name
used on the FORMAT parameter. The program is written as if this file contains
actual data.
If you read the file sequentially, your high-level language can automatically specify
that the key fields are ignored. Normally you write the program as if it is reading
records in arrival sequence. If the KEYFLD parameter is used on the Open Query
File (OPNQRYF) command, you receive a diagnostic message, which can be
ignored.
If you process the file randomly by keys, your high-level language probably
requires a key specification. If you have selection values, it can prevent your
program from accessing a record that exists in the database. A Record not found
condition can occur on a random read whether the OPNQRYF command was used
or whether a logical file created using DDS select/omit logic was used.
In some cases, you can monitor exceptions caused by mapping errors such as
arithmetic overflow, but it is better to define the attributes of all fields to correctly
handle the results.
To stop these status messages from appearing, see the discussion about message
handling in the CL Programming book.
When your job is running under debug (by using the STRDBG command), or
requested with query options file option of DEBUG_MESSAGES *YES, messages
are sent to your job log. These messages describe the implementation method that
is used to process the OPNQRYF request. These messages provide information
about the optimization processing that occurred. You can use these messages as a
tool for tuning the OPNQRYF request to achieve the best performance. The
messages are as follows:
CPI4321
Access path built for file...
CPI4322
Access path built from keyed file...
CPI4324
Temporary file built from file...
CPI4325
Temporary file built for query
CPI4326
File processed in join position...
CPI4327
File processed in join position 1.
CPI4328
Access path of file that is used...
CPI4329
Arrival sequence that is used for file...
CPI432A
Query optimizer timed out...
CPI432C
All access paths considered for file...
CPI432E
Selection fields mapped to different attributes...
CPI432F
Access path suggestion for file...
CPI433B
Unable to update query options file.
CPI4330
&6 tasks used for parallel &10 scan of file &1.
CPI4332
&6 tasks used for parallel index that is created over file...
CPI4333
Hashing algorithm used to process join.
Most of the messages provide a reason why the particular option was performed.
The second level text on each message gives an extended description of why the
option was chosen. Some messages provide suggestions to help improve the
performance of the OPNQRYF request.
If you want to change a field value from the current value to a different value in
some of the records in a file, you can use a combination of the OPNQRYF
command and a specific high-level language program. For example, assume you
want to change all the records where the Flda field is equal to ABC so that the Flda
field is equal to XYZ. You can specify:
OVRDBF FILE(FILEA) SHARE(*YES)
OPNQRYF FILE(FILEA) OPTION(*ALL) QRYSLT('FLDA *EQ "ABC" ')
CALL PGM(PGMA)
CLOF OPNID(FILEA)
DLTOVR FILE(FILEA)
Program PGMA processes all records it can read, but the query selection restricts
these to records where the Flda field is equal to ABC. The program changes the
field value in each record to XYZ and updates the record.
You can also delete records in a database file using the OPNQRYF command. For
example, assume you have a field in your record that, if equal to X, means the
You can also add records by using the OPNQRYF command. However, if the query
specifications include selection values, your program can be prevented from
reading the added records because of the selection values.
Date: The length of the field or literal must be at least 8 if the date format is *ISO,
*USA, *EUR, *JIS, *YMD, *MDY, or *DMY. If the date format is *JUL (yyddd), the
length of the variable must be at least 6 (includes the separator between yy and
ddd). The field or literal may be padded with blanks.
Time: For all of the time formats (*USA, *ISO, *EUR, *JIS, *HMS), the length of the
field or literal must be at least 4. The field or literal may be padded with blanks.
Labeled Duration
Date Duration
Time Duration
Timestamp Duration
The rules for the use of the subtraction operator on date and time values are not
the same as those for addition because a date or time value cannot be subtracted
from a duration, and because the operation of subtracting two date and time
values is not the same as the operation of subtracting a duration from a date or
time value. The specific rules governing the use of the subtraction operator with
date and time values follow:
v If the first operand is a date, the second operand must be a date, a date
duration, a string representation of a date, or a labeled duration of years,
months, or days.
v If the second operand is a date, the first operand must be a date or a string
representation of a date.
v If the first operand is a time, the second operand must be a time, a time
duration, a string representation of a time, or a labeled duration of hours,
minutes, or seconds.
v If the second operand is a time, the first operand must be a time or string
representation of a time.
v If the first operand is a timestamp, the second operand must be a timestamp, a
string representation of a timestamp, or a duration.
v If the second operand is a timestamp, the first operand must be a timestamp or
a string representation of a timestamp.
Date Arithmetic
Dates can be subtracted, incremented, or decremented.
Subtracting Dates: The result of subtracting one date (DATE2) from another
(DATE1) is a date duration that specifies the number of years, months, and days
between the two dates. The data type of the result is DECIMAL(8,0). If DATE1 is
greater than or equal to DATE2, DATE2 is subtracted from DATE1. If DATE1 is
less than DATE2, however, DATE1 is subtracted from DATE2, and the sign of the
result is made negative. The following procedural description clarifies the steps
involved in the operation RESULT = DATE1 - DATE2.
Adding or subtracting a duration of days will, of course, affect the day portion of
the date, and potentially the month and year.
Date durations, whether positive or negative, may also be added to and subtracted
from dates. As with labeled durations, the result is a valid date.
When adding durations to dates, adding one month to a given date gives the same
date one month later unless that date does not exist in the later month. In that case,
the date is set to that of the last day of the later month. For example, January 28
plus one month gives February 28; and one month added to January 29, 30, or 31
results in either February 28 or, for a leap year, February 29.
Note: If one or more months are added to a given date and then the same number
of months is subtracted from the result, the final date is not necessarily the
same as the original date.
Time Arithmetic
Times can be subtracted, incremented, or decremented.
Subtracting Times: The result of subtracting one time (TIME2) from another
(TIME1) is a time duration that specifies the number of hours, minutes, and
seconds between the two times. The data type of the result is DECIMAL(6,0). If
TIME1 is greater than or equal to TIME2, TIME2 is subtracted from TIME1. If
TIME1 is less than TIME2, however, TIME1 is subtracted from TIME2, and the sign
Time durations, whether positive or negative, also can be added to and subtracted
from times. The result is a time that has been incremented or decremented by the
specified number of hours, minutes, and seconds, in that order. TIME1 + X, where X
is a DECIMAL(6,0) number, is equivalent to the expression: TIME1 +
%DURHOUR(%HOUR(X)) + %DURMINUTE(%MINUTE(X)) +
%DURSEC(%SECOND(X))
Timestamp Arithmetic
Timestamps can be subtracted, incremented, or decremented.
The seconds and minutes part of the timestamps are subtracted as specified in the
rules for subtracting times:
The date part of the timestamp is subtracted as specified in the rules for
subtracting dates.
The best performance can occur when the Open Query File (OPNQRYF) command
uses an existing keyed sequence access path. For example, if you want to select all
the records where the Code field is equal to B and an access path exists over the
Code field, the system can use the access path to perform the selection (key
positioning selection) rather than read the records and select at run time (dynamic
selection).
The Open Query File (OPNQRYF) command cannot use an existing index when
any of the following are true:
v The key field in the access path is derived from a substring function.
v The key field in the access path is derived from a concatenation function.
If no access path exists over the Code field, the program reads all of the records in
the file and passes only the selected records to your program. That is, the file is
processed in arrival sequence.
The system can perform selection faster than your application program. If no
appropriate keyed sequence access path exists, either your program or the system
makes the selection of the records you want to process. Allowing the system to
perform the selection process is considerably faster than passing all the records to
your application program.
This is especially true if you are opening a file for update operations because
individual records must be passed to your program, and locks are placed on every
record read (in case your program needs to update the record). By letting the
system perform the record selection, the only records passed to your program and
locked are those that meet your selection values.
If you use the KEYFLD parameter to request a specific sequence for reading
records, the fastest performance results if an access path already exists that uses
the same key specification or if a keyed sequence access path exists that is similar
to your specifications (such as a key that contains all the fields you specified plus
some additional fields on the end of the key). This is also true for the GRPFLD
parameter and on the to-fields of the JFLD parameter. If no such access path exists,
the system builds an access path and maintains it as long as the file is open in
your job.
Processing all of the records in a file by an access path that does not already exist
is generally not as efficient as using a full record sort, if the number of records to
be arranged (not necessarily the total number of records in the file) exceeds 1000
and is greater than 20% of the records in the file. While it is generally faster to
If you do not intend to read all of the query records and if the OPTIMIZE
parameter is *FIRSTIO or *MINWAIT, you can specify a number to indicate how
many records you intend to retrieve. If the number of records is considerably less
than the total number the query is expected to return, the system may select a
faster access method.
If you use the grouping function, faster performance is achieved if you specify
selection before grouping (QRYSLT parameter) instead of selection after grouping
(GRPSLT parameter). Only use the GRPSLT parameter for comparisons involving
aggregate functions.
For most uses of the OPNQRYF command, new or existing access paths are used
to access the data and present it to your program. In some cases of the OPNQRYF
command, the system must create a temporary file. The rules for when a
temporary file is created are complex, but the following are typical cases in which
this occurs:
v When you specify a dynamic join, and the KEYFLD parameter describes key
fields from different physical files.
v When you specify a dynamic join and the GRPFLD parameter describes fields
from different physical files.
v When you specify both the GRPFLD and KEYFLD parameters but they are not
the same.
v When the fields specified on the KEYFLD parameter total more than 2000 bytes
in length.
v When you specify a dynamic join and *MINWAIT for the OPTIMIZE parameter.
v When you specify a dynamic join using a join logical file and the join type
(JDFTVAL) of the join logical file does not match the join type of the dynamic
join.
v When you specify a logical file and the format for the logical file refers to more
than one physical file.
v When you specify an SQL view, the system may require a temporary file to
contain the results of the view.
v When the ALWCPYDTA(*OPTIMIZE) parameter is specified and using a
temporary result would improve the performance of the query.
However, if the sort sequence table associated with the query is a unique-weight
sequence table (including *HEX), some additional optimization is possible. The
optimizer acts as though no sort sequence table is specified for any grouping fields
or any selection or join predicates that use the following operators or functions:
v *EQ
v *NE
v *CT
v %WLDCRD
v %VALUES
The advantage is that the optimizer is free to use any existing access path where
the keys match the field and the access path either:
v Does not contain a sequence table.
v Contains a unique-weight sequence table (the table does not have to match the
unique-weight sort sequence table associated with the query).
The selection functions done by the OPNQRYF command (for the QRYSLT and
GRPSLT parameters) are similar to logical file select/omit. The main difference is
that for the OPNQRYF command, the system decides whether to use access path
selection or dynamic selection (similar to omitting or specifying the DYNSLT
keyword in the DDS for a logical file), as a result of the access paths available on
the system and what value was specified on the OPTIMIZE parameter.
Fields that have any of the large object data types: BLOB, CLOB, or DBCLOB, can
only be read using the Copy From Query File (CPYFRMQRYF) command or
Structured Query Language (SQL). Large object field data cannot be directly
accessed from an open query file. TheCPYFRMQRYF command must be used to
access large object fields from an open query file. A field with a large object data
type (BLOB, CLOB or DBCLOB) cannot be specified on the following OPNQRYF
parameters: KEYFLD, UNIQUEKEY, JFLD, and GRPFLD.
Fields contained in a record format, identified on the FILE parameter, and defined
(in the DDS used to create the file) with a usage value of N (neither input nor
output) cannot be specified on any parameter of the OPNQRYF command. Only
fields defined as either I (input-only) or B (both input and output) usage can be
specified. Any fields with usage defined as N in the record format identified on the
FORMAT parameter are ignored by the OPNQRYF command.
Fields in the open query file records normally have the same usage attribute
(input-only or both input and output) as the fields in the record format identified
on the FORMAT parameter, with the exceptions noted below. If the file is opened
for any option (OPTION parameter) that includes output or update and any usage,
and if any B (both input and output) field in the record format identified on the
FORMAT parameter is changed to I (input only) in the open query file record
format, then an information message is sent by the OPNQRYF command.
The OPNQRYF command never shares an existing open data path in the job or
activation group. A request to open a query file fails with an error message if the
open data path has the same library, file, and member name that is in the open
request, and if either of the following is true:
v OPNSCOPE(*ACTGRPDFN) or OPNSCOPE(*ACTGRP) is specified for the
OPNQRYF command, and the open data path is scoped to the same activation
group or job from which the OPNQRYF command is run.
v OPNSCOPE(*JOB) is specified for the OPNQRYF command, and the open data
path is scoped to the same job from which the OPNQRYF command is run.
Subsequent shared opens adhere to the same open options (such as SEQONLY)
that were in effect when the OPNQRYF command was run.
See “Sharing Database Files in the Same Job or Activation Group” on page 108 for
more information about sharing files in a job or activation group.
Although the CPYFRMQRYF command uses the open data path of the open query
file, it does not open the file. Consequently, you do not have to specify
SHARE(*YES) for the database file you are copying.
The following are examples of how the OPNQRYF and CPYFRMQRYF commands
can be used.
Example 1: Copying from an Open Query File: Building a file with a subset of
records
Assume you want to create a file from the CUSTOMER/ADDRESS file containing
only records where the value of the STATE field is Texas. You can specify the
following:
OPNQRYF FILE(CUSTOMER/ADDRESS) QRYSLT('STATE *EQ "TEXAS"')
CPYFRMQRYF FROMOPNID(ADDRESS) TOFILE(TEXAS/ADDRESS) CRTFILE(*YES)
Assume you want to print all records from FILEA where the value of the CITY
field is Chicago. You can specify the following:
OPNQRYF FILE(FILEA) QRYSLT('CITY *EQ "CHICAGO"')
CPYFRMQRYF FROMOPNID(FILEA) TOFILE(*PRINT)
Assume you want to copy all records from FILEB where the value of FIELDB is 10
to a diskette. You can specify the following:
OPNQRYF FILE(FILEB) QRYSLT('FIELDB *EQ "10"') OPNID(MYID)
CPYFRMQRYF FROMOPNID(MYID) TOFILE(DISK1)
Example 4: Copying from an Open Query File: Creating a copy of the output of
a dynamic join
Assume you want to create a physical file that has the format and data of the join
of FILEA and FILEB. Assume the files contain the following fields:
FILEA FILEB JOINAB
Cust Cust Cust
Name Amt Name
Addr Amt
The join field is Cust, which exists in both files. To join the files and save a copy of
the results in a new physical file MYLIB/FILEC, you can specify:
OPNQRYF FILE(FILEA FILEB) FORMAT(JOINAB) +
JFLD((FILEA/CUST FILEB/CUST)) +
MAPFLD((CUST 'FILEA/CUST')) OPNID(QRYFILE)
CPYFRMQRYF FROMOPNID(QRYFILE) TOFILE(MYLIB/FILEC) CRTFILE(*YES)
The file MYLIB/FILEC will be created by the CPYFRMQRYF command. The file
will have file attributes like those of FILEA although some file attributes may be
changed. The format of the file will be like JOINAB. The file will contain the data
from the join of FILEA and FILEB using the Cust field. File FILEC in library
MYLIB can be processed like any other physical file with CL commands, such as
the Display Physical File Member (DSPPFM) command and utilities, such as
Query. For more information about the CPYFRMQRYF command and other copy
commands, see File Management.
These are the most common problems and how to correct them:
v Shared open data path (ODP). The OPNQRYF command operates through a
shared ODP. In order for the file to process correctly, the member must be
opened for a shared ODP. If you are having problems, use the open files option
on the DSPJOB command to determine if the member is opened and has a
shared ODP.
There are normally two reasons that the file is not open:
– The member to be processed must be SHARE(*YES). Either use an Override
with Database File (OVRDBF) command or permanently change the member.
– The file is closed. You have run the OPNQRYF command with the
OPNSCOPE(*ACTGRPDFN) or TYPE(*NORMAL) parameter option from a
program that was running in the default activation group at a higher level in
The file position is first set to the position specified in the POSITION parameter on
the Override with Database File (OVRDBF) command. If you do not use an
OVRDBF command, or if you take the default for the POSITION parameter, the file
position is set just before the first record in the member’s access path.
A program can change the current file position by using the appropriate high-level
language program file positioning operation (for example, SETLL in the RPG/400
language or START in the COBOL/400 language). A program can also change the
file position by using the CL Position Database File (POSDBF) command.
Note: File positioning by means of the Override with Database File (OVRDBF)
command does not occur until the next time the file is opened. Because a
file can be opened only once within a CL program, this command cannot be
used within a single CL program to affect what will be read through the
RCVF command.
At end of file, after the last read, the file member is positioned to *START or *END
file position, depending on whether the program was reading forward or
backward through the file. The following diagram shows *START and *END file
positions.
For sequential read operations, the current file position is used to locate the next or
previous record on the access path. For read-by-key or read-by-relative-record-
number operations, the file position is not used. If POSITION(*NONE) is specified
at open time, no starting file position is set. In this case, you must establish a file
position in your program, if you are going to read sequentially.
If end-of-file delay was specified for the file on an Override With Database File
(OVRDBF) command, the file is not positioned to *START or *END when the
program reads the last record. The file remains positioned at the last record read. A
file with end-of-file delay processing specified is positioned to *START or *END
only when a force-end-of-data (FEOD) occurs or a controlled job end occurs. For
more information about end-of-file delay, see “Waiting for More Records When
End of File Is Reached” on page 179.
You can also use the Position Database File (POSDBF) command to set or change
the current position in your file for files opened using either the Open Database
File (OPNDBF) command or the Open Query File (OPNQRYF) command.
Note: Your high-level language may not allow all of the following read operations.
Refer to your high-level language guide to determine which operations are
allowed by the language.
When a keyed sequence access path is used, a read operation cannot position to
the storage occupied by a deleted record.
Note: Your high-level language may not allow all of the following operations.
Refer to your high-level language guide to determine which operations are
allowed by the language.
Read when Logical File Shares an Access Path with More Keys
operation using a Keyed Sequence Access Path
When the FIFO, LIFO, or FCFO keyword is not specified in the data description
specifications (DDS) for a logical file, the logical file can implicitly share an access
path that has more keys than the logical file being created. This sharing of a partial
set of keys from an existing access path can lead to perceived problems for
database read operations that use these partially shared keyed sequence access
paths. The problems will appear to be:
v Records that should be read, are never returned to your program
v Records are returned to your program multiple times
If these consequences caused by partially shared keyed sequence access paths are
not acceptable, the FIFO, LIFO, or FCFO keyword can be added to the DDS for the
logical file, and the logical file created again.
Also, special consideration should be taken when using end-of-file delay on a file
with a keyed sequence access path, opened so that the keyed sequence access path
is used. In this case, once end-of-file is reached, the system retrieves only those
records added to the file or those records updated in the file that meet the
specification of the read operation using the keyed sequence access path.
For example, end-of-file delay is used on a keyed file that has a numeric key field
in ascending order. An application program reads the records in the file using the
keyed sequence access path. The application program performs a read next
operation and gets a record that has a key value of 99. The application program
performs another read next and no more records are found in the file, so the
system attempts to read the file again after the specified end-of-file delay time. If a
record is added to the file or a record is updated, and the record has a key value
less than 99, the system does not retrieve the record. If a record is added to the file
or a record is updated and the record has a key value greater than or equal to 99,
the system retrieves the record.
For end-of-file delay times equal to or greater than 10 seconds, the job is eligible to
be removed from main storage during the wait time. If you do not want the job
eligible to be moved from main storage, specify PURGE(*NO) on the Create Class
(CRTCLS) command for the CLASS the job is using.
To indicate which jobs have an end-of-file delay in progress, the status field of the
Work with Active Jobs (WRKACTJOB) display shows an end-of-file wait or
end-of-file activity level for jobs that are waiting for a record.
If a job uses end-of-file-delay and commitment control, it can hold its record locks
for a longer period of time. This increases the chances that some other job can try
to access those same records and be locked out. For that reason, be careful when
using end-of-file-delay and commitment control in the same job.
If a file is shared, the Override with Database File (OVRDBF) command specifying
an end-of-file delay must be requested before the first open of the file because
overrides are ignored that are specified after the shared file is opened.
There are several ways to end a job that is waiting for more records because of an
end-of-file-delay specified on the Override with Database File (OVRDBF)
command:
v Write a record to the file with the end-of-file-delay that will be recognized by the
application program as a last record. The application program can then specify a
force-end-of-data (FEOD) operation. An FEOD operation allows the program to
complete normal end-of-file processing.
v Do a controlled end of a job by specifying OPTION(*CNTRLD) on the End Job
(ENDJOB) command, with a DELAY parameter value time greater than the
EOFDLY time. The DELAY parameter time specified must allow time for the
EOFDLY time to run out, time to process any new records that have been put in
The actual processing of the EOFDLY parameter is more complex than shown
because it is possible to force a true end-of-file if OPTION(*CNTRLD) on the End
Job (ENDJOB) command is used with a long delay time.
The job does not become active whenever a new record is added to the file. The
job becomes active after the specified end-of-file delay time ends. When the job
becomes active, the system checks for any new records. If new records were added,
the application program gets control and processes all new records, then waits
again. Because of this, the job takes on the characteristic of a batch job when it is
processing. For example, it normally processes a batch of requests. When the batch
is completed, the job becomes inactive. If the delay is small, you can cause
Note: When the job is inactive (waiting) it is in a long-wait status, which means it
was released from an activity level. After the long-wait status is satisfied, the
system reschedules the job in an activity level. (See the Work Management
book for more information about activity levels.)
Note: The rules for locking are different if your job is running under commitment
control. See the Backup and Recovery book for more details.
If you issue several read operations with the update option specified, each read
operation releases the lock on the previous record before attempting to locate and
lock the new record. When you do the update operation, the system assumes that
you are updating the currently locked record. Therefore, you do not have to
identify the record to be updated on the update operation. After the update
operation is done, the system releases the lock.
Note: The rules for locking are different if your job is running under commitment
control. See the Backup and Recovery book for more details.
If the update operation changes a key field in an access path for which immediate
maintenance is specified, the access path is updated if the high-level language
allows it. (Some high-level languages do not allow changes to the key field in an
update operation.)
If you request a read operation on a record that is already locked for update and if
your job is running under a commitment control level of *ALL or *CS (cursor
stability), then you must wait until the record is released or the time specified by
the WAITRCD parameter on the create file or override commands has been
exceeded. If the WAITRCD time is exceeded without the lock being released, an
exception is returned to your program and a message is sent to your job stating
the file, member, relative record number, and the job which has the lock. If the job
that is reading records is not running under a commitment control level of *ALL or
*CS, the job is able to read a record that is locked for update.
If the files being updated are associated with referential constraints, the update
operation can be affected. See “Chapter 17. Ensuring data integrity with referential
constraints” on page 245 for detailed information on referential constraints.
The new record is normally added at the end of the physical file member. The next
available relative record number (including deleted records) is assigned to the new
record. Some high-level languages allow you to write a new record over a deleted
record position (for example, the WRITE statement in COBOL/400 when the file
organization is defined as RELATIVE). For more information about writing records
over deleted record positions, see your high-level language guide.
If the physical file to which records are added reuses deleted records, the system
tries to insert the records into slots that held deleted records. Before you create or
change a file to reuse deleted records, you should review the restrictions and tips
for use to determine whether the file is a candidate for reuse of deleted record
space. For more information on reusing deleted record space, see “Database file
processing: Reusing Deleted Records” on page 103.
If you are adding new records to a file member that has a keyed access path, the
new record appears in the keyed sequence access path immediately at the location
defined by the record key. If you are adding records to a logical member that
contains select/omit values, the omit values can prevent the new record from
appearing in the member’s access path.
If the file to which you are adding a record has an insert trigger associated with it,
the trigger program is called before or after inserting the record. See “Chapter 18.
Triggering automatic events in your database” on page 261 for detailed information
on trigger programs.
If the files you are adding to are associated with referential constraints, record
insertion can be affected. See “Chapter 17. Ensuring data integrity with referential
constraints” on page 245 for detailed information on referential constraints.
The SIZE parameter on the Create Physical File (CRTPF) and Create Source
Physical File (CRTSRCPF) commands determines how many records can be added
to a physical file member.
A format selector program must be used if all of the following are true:
v The logical file is not a join and not a view logical file.
v The logical file is based on multiple physical files.
v The program uses a file name instead of a record format name on the add
operation.
If you do not write a format selector program for this situation, your program ends
with an error when it tries to add a record to the database.
Note: A format selector program cannot be used to select a member if a file has
multiple members; it can only select a record format.
When an application program wants to add a record to the database file, the
system calls the format selector program. The format selector program examines
the record and specifies the record format to be used. The system then adds the
record to the database file using the specified record format name.
The following example shows the programming statements for a format selector
program written in the RPG/400 language:
CL0N01N02N03Factor1+++OpcdeFactor2+++ResultLenDHHiLoEqComments+++...
+++*
C *ENTRY PLIST
C PARM RECORD 80
C* The length of field RECORD must equal the length of
C* the longest record expected.
C PARM FORMAT 10
C MOVELRECORD BYTE 1
C BYTE IFEQ 'A'
C MOVEL'HDR' FORMAT
C ELSE
C MOVEL'DTL' FORMAT
C END
The format selector uses the second parameter, which is a 10-character field, to
pass the record format name to the system. When the system knows the name of
the record format, it adds the record to the database.
To create the format selector, you use the create program command for the
language in which you wrote the program. You cannot specify USRPRF(*OWNER)
on the create command. The format selector must run under the user’s user profile
not the owner’s user profile.
In addition, for security and integrity and because performance would be severely
affected, you must not have any calls or input/output operations within the format
selector.
The name of the format selector is specified on the FMTSLR parameter of the
Create Logical File (CRTLF), Change Logical File (CHGLF), or Override with
Database File (OVRDBF) command. The format selector program does not have to
exist when the file is created, but it must exist when the application program is
run.
The force-end-of-data (FEOD) operation also allows you to position to either the
beginning or the end of a file if the file is open for input operations. *START sets
the beginning or starting position in the database file member currently open to
just before the first record in the member (the first sequential read operation reads
the first record in the current member). If MBR(*ALL) processing is in effect for the
override with Database File (OVRDBF) command, a read previous operation gets
the last record in the previous member. If a read previous operation is done and
the previous member does not exist, the end of file message (CPF5001) is sent.
*END sets the position in the database file member currently open to just after the
last record in the member (a read previous operation reads the last record in the
current member). If MBR(*ALL) processing is in effect for the Override with
Database File (OVRDBF) command, a read next operation gets the first record in
If the file has a delete trigger, the force-end-of-data operation is not allowed. See
“Chapter 18. Triggering automatic events in your database” on page 261 for
detailed information on triggers. If the file is part of a referential parent
relationship, the FEOD operation will not be allowed. See “Chapter 17. Ensuring
data integrity with referential constraints” on page 245 for detailed information on
referential constraints.
See your high-level language guide for more information about the FEOD
operation (some high-level languages do not support the FEOD operation).
Note: Some high-level languages do not require that you read the record first.
These languages allow you to simply specify which record you want deleted
on the delete statement. For example, the RPG/400 language allows you to
delete a record without first reading it.
When a database record is deleted, the physical record is marked as deleted. This
is true even if the delete operation is done through a logical file. A deleted record
cannot be read. The record is removed from all keyed sequence access paths that
contain the record. The relative record number of the deleted record remains the
same. All other relative record numbers within the physical file member do not
change.
The space used by the deleted record remains in the file, but it is not reused until:
v The Reorganize Physical File Member (RGZPFM) command is run to compress
and free these spaces in the file member. See “Reorganizing a Table” on page 197
for more information about this command.
v Your program writes a record to the file by relative record number and the
relative record number used is the same as that of the deleted record.
Note: The system tries to reuse deleted record space automatically if the file has
the reuse deleted record space attribute specified. For more information, see
“Database file processing: Reusing Deleted Records” on page 103.
The system does not allow you to retrieve the data for a deleted record. You can,
however, write a new record to the position (relative record number) associated
with a deleted record. The write operation replaces the deleted record with a new
record. See your high-level language guide for more details about how to write a
record to a specific position (relative record number) in the file.
If the file from which you are deleting has a delete trigger associated with it, the
trigger program is called before or after deleting the record. See “Chapter 18.
Triggering automatic events in your database” on page 261 for detailed information
on triggers.
Most high-level languages allow you to specify that you want to close your
database files. For more information about how to close a database file in a
high-level language program, see your high-level language guide.
You can use the Close File (CLOF) command to close database files that were
opened using either the Open Database File (OPNDBF) or Open Query File
(OPNQRYF) commands.
You can also close database files by running the Reclaim Resources (RCLRSC)
command. The RCLRSC command releases all locks (except, under commitment
control, locks on records that were changed but not yet committed), forces all
changes to auxiliary storage, then destroys the open data path for that file. You can
use the RCLRSC command to allow a calling program to close a called program’s
files. (For example, if the called program returns to the calling program without
closing its files, the calling program can then close the called program’s files.)
However, the normal way of closing files in a program is with the high-level
language close operation or through the Close File (CLOF) command. For more
information on resource reclamation in the integrated language environment see
the ILE Concepts book.
If a job ends normally (for example, a user signs off) and all the files associated
with that job were not closed, the system automatically closes all the remaining
open files associated with that job, forces all changes to auxiliary storage, and
releases all record locks for those files. If a job ends abnormally, the system also
closes all files associated with that job, releases all record locks for those files, and
forces all changes to auxiliary storage.
| One or more of the following events occurs when error conditions are detected
| during processing of a database file:
| v Messages can be sent to the program message queue for the program processing
| the file.
| v An inquiry message can be sent to the system operator message queue.
| v File errors and diagnostic information can appear to your program as return
| codes and status information in the file feedback area.
| For example, the COBOL for AS/400 language sets a return code in the file status
| field, if it is defined in the program.
If you do not monitor for any of these messages, the system handles the error. The
system also sets the appropriate error return code in the program. Depending on
the error, the system can end the job or send a message to the operator requesting
further action.
If a message is sent to your program while processing a database file member, the
position in the file is not lost. It remains at the record it was positioned to before
the message was sent, except:
v After an end-of-file condition is reached and a message is sent to the program,
the file is positioned at *START or *END.
v After a conversion mapping message on a read operation, the file is positioned
to the record containing the data that caused the message.
If your programming language allows you to monitor for error messages, you can
choose which ones you wish to monitor for. The following messages are a small
sample of the error messages you can monitor (see your high-level language guide
and the CL Reference for a complete list of errors and messages that you can
monitor). To display the full description of these messages, use the Display
Message Description (DSPMSGD) command.
Also included is information to help you plan for recovery of your database files in
the event of a system failure:
v Saving and restoring
v Journaling
v Using auxiliary storage
v Using commitment control
This section also has information on access path recovery that includes rebuilding
and journaling access paths.
A section on source files discusses source file concepts and reasons you would use
a source file. Information on how to set up a source file, how to enter data into a
source file, and ways to use a source file to create another object on the system is
included.
If a file contains more than one member, each member serves as a subset of the
data in the file. This allows you to classify data easier. For example, you define an
accounts receivable file. You decide that you want to keep data for a year in that
file, but you frequently want to process data just one month at a time. For
example, you create a physical file with 12 members, one named for each month.
Then, you process each month’s data separately (by individual member). You can
also process several or all members together.
Note: You can use the Change Physical File (CHGPF) and Change Logical File
(CHGLF) commands to change many other file attributes. For example, to
change the maximum size allowed for each member in the file, you would
use the SIZE parameter on the CHGPF command.
Renaming Members
The Rename Member (RNMM) command changes the name of an existing member
in a physical or logical file. The file name is not changed.
If the file member being operated on is associated with referential constraints, the
operation can be affected. See “Chapter 17. Ensuring data integrity with referential
constraints” on page 245 for detailed information on referential constraints.
You can use the Initialize Physical File Member (INZPFM) command to initialize
members with one of two types of records:
v Default records
v Deleted records
You specify which type of record you want using the RECORDS parameter on the
Initialize Physical File Member (INZPFM) command.
If you initialize records using default records, the fields in each new record are
initialized to the default field values defined when the file was created. If no
default field value was defined, then numeric fields are filled with zeros and
character fields are filled with blanks.
Note: You can initialize one default record if the UNIQUE keyword is specified in
DDS for the physical file member or any associated logical file members.
Otherwise, you would create a series of duplicate key records.
If the records are initialized to the default records, you can read a record by
relative record number and change the data.
If the records were initialized to deleted records, you can change the data by
adding a record using a relative record number of one of the deleted records. (You
cannot add a record using a relative record number that was not deleted.)
Deleted records cannot be read; they only hold a place in the member. A deleted
record can be changed by writing a new record over the deleted record. Refer to
“Deleting Database Records” on page 186 for more information about processing
deleted records.
Reorganizing a Table
| You can reorganize a table using the Reorganize Table operation in Operations
| Navigator. Or, you can use the Reorganize Physical File Member (RGZPFM)
| command.
You can use the Reorganize Physical File Member (RGZPFM) command to:
v Remove deleted records to make the space occupied by them available for more
records.
v Reorganize the records of a file in the order in which you normally access them
sequentially, thereby minimizing the time required to retrieve records. This is
done using the KEYFILE parameter. This may be advantageous for files that are
primarily accessed in an order other than arrival sequence. A member can be
reorganized using either of the following:
– Key fields of the physical file
– Key fields of a logical file based on the physical file
v Reorganize a source file member, insert new source sequence numbers, and reset
the source date fields (using the SRCOPT and SRCSEQ parameters on the
Reorganize Physical File Member command).
v Reclaim space in the variable portion of the file that was previously used by
variable-length fields in the physical file format and that has now become
fragmented.
The physical file ORDHDRP has an arrival sequence access path. It was
reorganized using the access path in the logical file ORDFILL. Assume the key
field is the Order field. The following illustrates how the records were arranged.
The following is an example of the original ORDHDRP file. Note that record 3 was
deleted before the RGZPFM command was run:
The following example shows the ORDHDRP file reorganized using the Order field
as the key field in ascending sequence:
If one of the following conditions occur and the Reorganize Physical File Member
(RGZPFM) command is running, the records may not be reorganized:
v The system ends abnormally.
v The job containing the RGZPFM command is ended with an *IMMED option.
The status of the member being reorganized depends on how much the system
was able to do before the reorganization was ended and what you specified in the
SRCOPT parameter. If the SRCOPT parameter was not specified, the member is
either completely reorganized or not reorganized at all. You should display the
contents of the file, using the Display Physical File Member (DSPPFM) command,
to determine if it was reorganized. If the member was not reorganized, issue the
Reorganize Physical File Member (RGZPFM) command again.
If the SRCOPT parameter was specified, any of the following could have happened
to the member:
v It was completely reorganized. A completion message is sent to your job log
indicating the reorganize operation was completely successful.
v It was not reorganized at all. A message is sent to your job log indicating that
the reorganize operation was not successful. If this occurs, issue the Reorganize
Physical File Member (RGZPFM) command again.
v It was reorganized, but only some of the sequence numbers were changed. A
completion message is sent to your job log indicating that the member was
reorganized, but all the sequence numbers were not changed. If this occurs, issue
the RGZPFM command again with KEYFILE(*NONE) specified.
To reduce the number of deleted records that exist in a physical file member, the
file can be created or changed to reuse deleted record space. For more information,
see “Database file processing: Reusing Deleted Records” on page 103.
You can display source files or data files, regardless if they are keyed or arrival
sequence. Records are displayed in arrival sequence, even if the file is a keyed file.
You can page through the file, locate a particular record by record number, or shift
the display to the right or left to see other parts of the records. You can also press
a function key to show either character data or hexadecimal data on the display.
If you have Query installed, you can use the Start Query (STRQRY) command to
select and display records, too.
If you have the SQL language installed, you can use the Start SQL (STRSQL)
command to interactively select and display records.
The system assigns a unique level identifier for each record format when the file it
is associated with is created. The system uses the information in the record format
description to determine the level identifier. This information includes the total
length of the record format, the record format name, the number and order of
fields defined, the data type, the size of the fields, the field names, and the number
of decimal positions in the field. Changes to this information in a record format
cause the level identifier to change.
The following DDS information has no effect on the level identifier and, therefore,
can be changed without recompiling the program that uses the file:
v TEXT keyword
v COLHDG keyword
v CHECK keyword
v EDTCDE keyword
v EDTWRD keyword
v REF keyword
v REFFLD keyword
v CMP, RANGE, and VALUES keywords
v TRNTBL keyword
v REFSHIFT keyword
v DFT keyword
v CCSID keyword
v ALWNULL keyword
v Join specifications and join keywords
v Key fields
v Access path keywords
v Select/omit fields
Keep in mind that even though changing key fields or select/omit fields will not
cause a level check, the change may cause unexpected results in programs using
the new access path. For example, changing the key field from the customer
number to the customer name changes the order in which the records are
retrieved, and may cause unexpected problems in the programs processing the file.
The format level identifier defined in the file can be displayed by the Display File
Description (DSPFD) command. When you are displaying the level identifier,
remember that the record format identifier is compared, rather than the file
identifier.
Not every change in a file necessarily affects your program. For example, if you
add a field to the end of a file and your program does not use the new field, you
do not have to recompile your program. If the changes do not affect your program,
you can use the Change Physical File (CHGPF) or the Change Logical File
(CHGLF) commands with LVLCHK(*NO) specified to turn off level checking for
the file, or you can enter an Override with Database File (OVRDBF) command
with LVLCHK(*NO) specified so that you can run your program without level
checking.
Keep in mind that level checking is the preferred method of operating. The use of
LVLCHK(*YES) is a good database integrity practice. The results produced by
LVLCHK(*NO) cannot always be predicted.
You can avoid compiling again by creating a logical file that presents data to your
programs in the original record format of the physical file. Using this approach, the
logical file has the same level check identifier as the physical file before the change.
For example, you decide to add a field to a physical file record format. You can
avoid compiling your program again by doing the following:
1. Change the DDS and create a new physical file (FILEB in LIBA) to include the
new field:
CRTPF FILE(LIBA/FILEB) MBR(*NONE)...
FILEB does not have a member. (The old file FILEA is in library LIBA and has
one member MBRA.)
2. Copy the member of the old physical file to the new physical file:
CPYF FROMFILE(LIBA/FILEA) TOFILE(LIBA/FILEB)
FROMMBR(*ALL) TOMBR(*FROMMBR)
MBROPT(*ADD) FMTOPT(*MAP)
The following illustrates the relationship of the record formats used in the three
files:
When you make changes to a physical file that cause you to create the file again,
all logical files referring to it must first be deleted before you can delete and create
the new physical file. After the physical file is re-created, you can re-create or
restore the logical files referring to it. The following examples show two ways to
do this.
You can use the Change Physical File (CHGPF) command to change some of the
attributes of a physical file and its members. For information on these parameters,
see the Change Physical File (CHGPF) command in the CL Reference.
To avoid recompiling, you can keep the current logical file (unchanged) and create
a new logical file with the added field. Your program refers to the old file, which
still exists.
You can use the Change Logical File (CHGLF) command to change most of the
attributes of a logical file and its members that were specified on the Create
Logical File (CRTLF) command.
Each of the commands described in the following sections can present information
on a display, a printout, or write the cross-reference information to a database file
that, in turn, can be used by a program or utility (for example, Query) for analysis.
For more information about writing the output to a database file, see “Writing the
Output from a Command Directly to a Database File” on page 211.
You can retrieve information about a member of a database file for use in your
applications with the Retrieve Member Description (RTVMBRD) command. See the
section on “Retrieving Member Description Information” in the CL Programming for
an example of how the RTVMBRD command is used in a CL program to retrieve
the description of a specific member.
For example, to display a list of all database files associated with physical file
ORDHDRP, with the record format ORDHDR, type the following DSPDBR
command:
DSPDBR FILE(DSTPRODLB/ORDHDRP) RCDFMT(ORDHDR)
Note: See the DSPDBR command description in the CL Reference for details of this
display.
This display presents header information when a record format name is specified
on the RCDFMT parameter, and presents information about which files are using
the specified record format.
If the Display Database Relations (DSPDBR) command is specified with the default
MBR(*NONE) parameter value, the dependent data files are shown. To display the
shared access paths, you must specify a member name.
The Display Database Relations (DSPDBR) command output identifies the type of
sharing involved. If the results of the command are displayed, the name of the
type of sharing is displayed. If the results of the command are written to a
database file, the code for the type of sharing (shown below) is placed in the
WHTYPE field in the records of the output file.
When a program is created, the information about certain objects used in the
program is stored. This information is then available for use with the Display
Program References (DSPPGMREF) command.
The following chart shows the objects for which the high-level languages and
utilities save information:
The stored file information contains an entry (a number) for the type of use. In the
database file output of the Display Program References (DSPPGMREF) command
(built when using the OUTFILE parameter), this is specified as:
Code Meaning
1 Input
2 Output
3 Input and Output
4 Update
8 Unspecified
Combinations of codes are also used. For example, a file coded as a 7 would be
used for input, output, and update.
Note: The authority to use these files is restricted to the security officer. However,
all users have authority to view the data by using one of (or the only)
You can use the output files in programs or utilities (for example, Query) for data
analysis. For example, you can send the output of the Display Program References
(DSPPGMREF) command to a physical file, then query that file to determine which
programs use a specific file.
The physical files are created for you when you specify the OUTFILE parameter on
the commands. Initially, the files are created with private authority; only the owner
(the person who ran the command) can use it. However, the owner can authorize
other users to these files as you would for any other database file.
The system supplies model files that identify the record format for each command
that can specify the OUTFILE parameter. If you specify a file name on the
OUTFILE parameter for a file that does not already exist, the system creates the
file using the same record format as the model files. If you specify a file name for
an existing output file, the system checks to see if the record format is the same
record format as the model file. If the record formats do not match, the system
sends a message to the job and the command does not complete.
Note: You must use your own files for output files, rather than specifying the
system-supplied model files on the OUTFILE parameter.
See the CL Reference for a list of commands that allow output files and the names
of the model files supplied for those commands.
Note: All system-supplied model files are located in the QSYS library.
You can display the fields contained in the record formats of the system-supplied
model files using the Display File Field Descriptions (DSPFFD) command.
You can use Query to process the output file. Another way to process the output
file is to create a logical file to select information from the file. The following is the
DDS for such a logical file. Records are selected based on the file name.
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
A* Logical file DBROUTL for query
A
A R DBROUTL PFILE(DBROUT)
A S WHFNAM VALUES('ORDHDRL' 'ORDFILL')
A
To collect access path information about all files in the LIBA library, you could
specify:
DSPFD FILE(LIBA/*ALL) TYPE(*ACCPTH) OUTPUT(*OUTFILE) +
OUTFILE(LIBB/ABC)
The file ABC is created in library LIBB and is externally described with the same
field descriptions as in the system-supplied file QSYS/QAFDACCP. The ABC file
then contains a record for each key field in each file found in library LIBA that has
an access path.
the file DEF is created in library LIBB and is externally described with the same
field descriptions as exist in QSYS/QAFDPHY. The DEF file then contains a record
for each physical file found in library LIBX.
You can display the field names of each model file supplied by IBM using the
DSPFFD command. For example, to display the field description for the access
path model file (*ACCPTH specified on the TYPE parameter), specify the
following:
DSPFFD QSYS/QAFDACCP
The records in all five output files have a problem identifier so that the cause, fix,
user text information, and supporting data can be correlated with the basic
For more information about save and restore on AS/400, see the Backup and
Recovery book and the following topics:
v “Database save and restore” on page 225
v “Database considerations for save and restore” on page 226
Managing journals
Journal management allows you to record all the data changes occurring to one or
more database files. You can then use the journal for recovery.
Journals
Journals allow you to record all the data changes to one or more database files.
You can then use the journal for recovery. If a database file is destroyed or
becomes unusable and you are using journals, you can reconstruct most of the
activity for the file. The journal also allows you to remove revisions made to the
file.
When a change is made to a file and you are using journals, the system records the
change in a journal receiver and writes the receiver to auxiliary storage before it is
recorded in the file. Therefore, the journal receiver always has the latest database
information.
Journal entries record activity for a specific record or for the file as a whole. Each
entry includes bytes of control information that identify the source of the activity
(such as user, job, program, time, and date). For changes that affect a single record,
record images are included after the control information. The record image before
the change can also be included. You can control whether to create a journal both
before and after record images or just after record images by specifying the
IMAGES parameter on the Start Journal Physical File (STRJRNPF) command.
All journaled database files are automatically synchronized with the journal when
the system is started (IPL time). If the system session ended abnormally, some
database changes may be in the journal, but some of these changes may not be
reflected in the database files. If that is the case, the system automatically updates
the database files from the journal.
Journals make saving database files an easier and faster task. For example, instead
of saving an entire file every day, simply save the journal receiver that contains the
changes to that file. You might still save the entire file on a weekly basis. This
method can reduce the amount of time it takes to perform your daily save
operations.
For more information about journals, refer to the book Backup and Recovery,
SC41-5304-04 .
| Transactions
Revisions made to files during transaction processing are journaled when using
commitment control.
If the system or job ends abnormally, journaling alone can ensure that, at most,
only the very last record change is lost. However, if the system or job ends
abnormally during a complex transaction, the files reflect an incomplete logical
transaction. For example, the job may have updated a record in file A, but before it
updated a corresponding record in file B, the job ended abnormally. In this case,
the logical transaction consisted of two updates, but only one update completed
before the job ended abnormally.
The commit and rollback operations are available in several AS/400 programming
languages including the RPG/400, COBOL/400, PL/I, SQL, and the AS/400
control language (CL).
You can open logical files for output under commitment control when underlying
physical files are journaled to different journals. However, the checks for violations
are deferred if a record change affects underlying physical files that are journaled
to the same journal. If the record change affects underlying physical files that are
not journaled to the same journal, and it causes a duplicate key or referential
constraint violation, an error will occur during the I/O operation. For example,
assume physical file A with a unique key is journaled to journal X, while physical
file B with a unique key is journaled to journal Y. Logical file C is created over
Access path recovery can take a long time, especially if you have large access paths
or many access paths to be rebuilt. You can reduce this recovery time in several
ways.
Journaling access paths is often faster than rebuilding access paths. With the
System-managed access path protection (SMAPP) support, you do not have to use
the journaling commands, such as STRJRNAP, to get the benefits of access path
journaling. SMAPP support recovers access paths after an abnormal system end
rather than rebuilding them during IPL.
The following topics describe in more detail how you can reduce access path
recovery time:
v “Saving access paths”
v “Restoring access paths” on page 219
v “Journaling access paths” on page 219
v “System-managed access-path protection (SMAPP)” on page 220
v “Rebuilding access paths” on page 220
Note that the logical file itself is not saved when you have specified the
ACCPTH(*YES) parameter. You have to save the logical file explicitly. For more
information, see the Backup and Recovery book.
For example, assume that a logical file is built over a physical file that contains
500,000 records. You have determined through the Display Object Description
(DSPOBJD) command that the size of the logical file is about 15 megabytes.
In this example, it takes about 50 minutes to rebuild the access path for the logical
file. It takes about 1 minute to restore the same access path from a tape. (This
assumes that the system builds approximately 10,000 index entries per minute.)
After restoring the access path, you may need to update the file by applying the
latest journal changes. For example, the system applies approximately 80,000 to
100,000 journal entries to the file per hour. This assumes that each of the physical
files to which entries are being applied has only one access path built over it. This
rate will drop proportionally for each access path of *IMMED maintenance that is
present over the physical file. Even with this additional recovery time, you will
usually find that it is faster to restore access paths rather than to rebuild them.
When you journal database files, you record images of changes to the file in the
journal. The system uses these record images to recover the file after an abnormal
system end.
After an abnormal end, the system may find that access paths are not synchronized
with the data in the file. If an access path does not synchronize with its data, the
system rebuilds the access path to ensure that the two are synchronized and
usable.
When journaling access paths, the system records images of the access path in the
journal to provide known synchronization points between the access path and its
data. By having that information in the journal, the system recovers both the data
files and the access paths. The system then synchronizes the two. In such cases,
you avoid the lengthy time to rebuild access paths.
In addition, other system recovery functions work with journaling access paths.
For example, the system has a number of options to reduce recovery time from the
failure and replacement of a disk unit. These options include user auxiliary storage
pools and checksum protection. These options further reduce the chances that the
entire system must reload because of the disk failure. However, you may still need
to rebuild access paths when the system is started following replacement of the
Before journaling an access path, you must journal the physical files that are
associated with the access path. In addition, you must use the same journal for the
access path and its associated physical files. It is easy to start journaling access
paths:
v You can use the system-managed access-path protection (SMAPP) facility.
v You can manage the journaling environment yourself with the Start Journal
Access Path (STRJRNAP) command.
– To start journaling the access path for the specified file, use the STRJRNAP
command. You can journal access paths that have a maintenance attribute of
immediate (*IMMED) or delayed (*DLY).
– Once you start journaling, the system protects the access path until the access
path is deleted or until you run the End Journal Access Path (ENDJRNAP)
command for that access path.
Access path journaling minimizes additional output operations. For example, the
system will write the journal data for the changed record and the changed access
path in the same output operation. However, you should seriously consider
isolating your journal receivers in user auxiliary storage pools when you start
journaling your access paths. Placing journal receivers in their own user auxiliary
storage pool provides the best journaling performance, while helping to protect
them from a disk failure. See the Backup and Recovery book for more information
about journaling access paths.
The following factors affect the time estimate when rebuilding access paths:
v Storage pool size. You can improve the rebuild time by running the job in a
larger storage pool.
v The system model. The speed of the processing unit is a key factor.
The following topics describe in more detail the techniques involved with
rebuilding access paths:
v “Controlling when access paths are rebuilt”
v “Designing files to reduce access path rebuilding time” on page 222
v “Other methods to avoid rebuilding access paths” on page 222
The IPL threshold value determines which access paths to rebuild during the IPL.
All access paths with a sequence value that is less than or equal to the IPL
threshold value rebuild during the IPL. Changing the IPL threshold value to 99
means that all access paths with a sequence value of 1 through 99 rebuild during
the IPL. Changing the IPL threshold value to 0 means that no access paths rebuild
until after the system completes its IPL, except those access paths that were being
journaled and those access paths for system files.
The access path recovery value for a file is determined by the value you specified
for the RECOVER parameter on the create and change file commands. The default
recovery value for *IPL (rebuild during IPL) is 25 and the default value for AFTIPL
(rebuild after IPL) is 75; therefore, RECOVER(*IPL) will show as 25. The initial IPL
threshold value is 50; this allows the parameters to affect when the access path is
rebuilt. You can override this value on the Edit Rebuild of Access Paths display.
For example, you can specify that all files that must have their access paths rebuilt
should rebuild the access paths when the file is first used. In this case, no access
paths are rebuilt at IPL. You can control the order in which the access paths are
rebuilt by running only those programs that use the files you want to rebuild first.
This method shortens the IPL time and could make the first of several applications
available faster. However, the overall time to rebuild access paths probably is
longer. (Other work may be running when the access paths are being rebuilt, and
there may be less main storage available to rebuild the access paths).
However, if the system abnormally ended during the day, the access path to the
smaller transaction file might need to be rebuilt. Still, the access path to the large
history file, being read-only for most of the day, would rarely be unsynchronized
with its data. Therefore, you reduce the chance of rebuilding this access path.
Consider the trade-off between using a file design to reduce access path rebuilding
time and using system-supplied functions like access path journaling. The above
file design may require a more complex application design. After evaluating your
situation, you may decide to use system-supplied functions like journaling your
access paths rather than design applications that are more complex.
You need to periodically synchronize the data with its access path to reduce the
number of access paths you must rebuild. There are several methods to
synchronize a file with its access path:
v Full file close. The last full (that is, not shared) system-wide close performed
against a file will synchronize the access path and the data.
v Force access path. Specify the force-access-path (FRCACCPTH) parameter on the
create, change, or override database file commands.
v Force write ratio of 2 or greater. Specify the force-write-ratio (FRCRATIO)
parameter on the create, change, or override database file commands.
Performing one of the methods mentioned previously synchronizes the access path
and the data. However, the next change to the data in the file can turn the
synchronization indicator off again.
Note that each of the methods can be costly in terms of performance; therefore, use
them with caution. Consider journaling access paths along with saving access
paths or using SMAPP as the primary means of protecting access paths.
Note: If you are not using journaling for a file, records may or may not exist after
IPL recovery, as follows:
v For added records, if after the IPL recovery the Nth record added exists, then all
records added preceding N also exist.
v For updated and deleted records, if the update or delete to the Nth record is
present after the IPL recovery, there is no guarantee that the records updated or
deleted prior to the Nth record are also present in the database.
v For REUSEFLT(*YES), records added are treated as updates, and these records
may not exist after IPL recovery.
A shared pool paging option other than *FIXED can have an impact on data loss
for nonjournaled physical files in a system failure. When you do not journal
physical files, data loss from a system failure, where memory is not saved, can
increase for *CALC or USRDFN paging options. You may write file changes to
auxiliary storage less frequently for these options. There is a risk of data loss for
nonjournaled files with the *FIXED option, but the risk can be higher for *CALC or
user defined (USRDFN) paging options.
For more information on the paging option see the ″Automatic System Tuning″
section of the Work Management book.
Save files are disk-resident files that can be the target of a save operation or the
source of a restore operation. Save files allow unattended save operations. An
operator does not need to load tapes or diskettes when saving to a save file.
However, periodically use the Save Save File Data (SAVSAVFDTA) command to
To create the following objects, source files can be used, but are not required:
v Physical files
v Display files
v Printer files
A source file can be a database file, diskette file, tape file, or inline data file. (An
inline data file is included as part of a job.) A source database file is simply
another type of database file. You can use a source database file like you would
any other database file on the system.
The CRTSRCPF command creates a physical file, but with attributes appropriate
for source physical files. For example, the default record length for a source file is
92 (80 for the source data field, 6 for the source sequence number field, and 6 for
the source date field).
The following example shows how to create a source file using the CRTSRCPF
command and using the command defaults:
You can either add your source members to these files or create your own source
files. Normally, you will want to create your own source files using the same
names as the IBM-supplied files, but in different libraries (IBM-supplied files may
get overlaid when a new release of the system is installed). The IBM-supplied
source files are created with the file names used for the corresponding create
command (for example, the CRTCLPGM command uses the QCLSRC file name as
the default). Additionally, the IBM-supplied programmer menu uses the same
default names. If you create your own source files, do not place them in the same
library as the IBM-supplied source files. (If you use the same file names as the
IBM-supplied names, you should ensure that the library containing your source
files precedes the library containing the IBM-supplied source files in the library
list.)
A record format consisting of the following three fields is automatically used for a
source physical file created using the Create Source Physical File (CRTSRCPF)
command:
Note: For all IBM-supplied database source files, the length of the data portion is
80 bytes. For IBM-supplied device source files, the length of the data portion
is the maximum record length for the associated device.
If you use SEU to update a source file, you can add records between existing
records. For example, if you add a record between records 0003.00 and 0004.00, the
sequence number of the added record could be 0003.01. SEU will automatically
arrange the newly added statements in this way.
When records are first placed in a source file, the date field is all zoned decimal
zeros (unless DDS is used with the DFT keyword specified). If you use SEU, the
date field changes in a record when you change the record.
If you open source device files for input, the system adds the sequence number
and date fields, but there are zeros in the date fields.
If you open a device file for output and the file is defined as a source file, the
system deletes the sequence number and date before writing the data to the device.
When you are copying from a database source file to another database source file
that has an insert trigger associated with it, the trigger program is called for each
record copied.
Using the Copy File (CPYF) Command for Copying to and from
Files
The CPYF command provides additional functions over the CPYSRCF command
such as:
v Copying from database source files to device files
v Copying from device files to database source files
v Copying between database files that are not source files and source database
files
v Printing a source member in hexadecimal format
v Copying source with selection values
If a starting value of .01 and an increased value of .01 are specified, the maximum
number of records that can have unique sequence numbers is 999,999. When the
maximum sequence number (9999.99) is reached, any remaining records will have
a sequence number of 9999.99.
The following is an example of copying source from one member to another in the
same file. If MBRB does not exist, it is added; if it does exist, all records are
replaced.
CPYSRCF FROMFILE(QCLSRC) TOFILE(QCLSRC) FROMMBR(MBRA) +
TOMBR(MBRB)
The following is an example of copying a generic member name from one file to
another. All members starting with PAY are copied. If the corresponding members
do not exist, they are added; if they do exist, all records are replaced.
CPYSRCF FROMFILE(LIB1/QCLSRC) TOFILE(LIB2/QCLSRC) +
FROMMBR(PAY*)
The following is an example of copying the member PAY1 to the printer file
QSYSPRT (the default for *PRINT). A format similar to the one used by SEU is
used to print the source statements.
CPYSRCF FROMFILE(QCLSRC) TOFILE(*PRINT) FROMMBR(PAY1)
When you copy from a device source file to a database source file, sequence
numbers are added and dates are initialized to zeros. Sequence numbers start at
1.00 and are increased by 1.00. If the file being copied has more than 9999 records,
then the sequence number is wrapped back to 1.00 and continues to be increased
unless the SRCOPT and SRCSEQ parameters are specified.
To export AS/400 database data to another system, use the Copy To Import File
(CPYTOIMPF) command to copy the data from your database file to the import
file. Then send the data to the system to which you are exporting the data.
For more information about importing and exporting database files, see the DB2
information in the AS/400 Information Center.
Source files are externally described database files. As such, when you name a
source file in your program and compile it, the source file description is
automatically included in your program printout. For example, assume you
wanted to read and update records for a member called FILEA in the source file
QDDSSRC. When you write the program to process this file, the system will
include the SRCSEQ, SRCDAT, and SRCDTA fields from the source file.
Note: You can display the fields defined in a file by using the Display File Field
Description command (DSPFFD). For more information about this
command, see “Displaying the Descriptions of the Fields in a File” on
page 208.
The program processing the FILEA member of the QDDSSRC file could:
For example, to create a CL program, you use the Create Control Language
Program (CRTCLPGM) command. A create command specifies through a SRCFILE
parameter where the source is stored.
The create commands are designed so that you do not have to specify source file
name and member name if you do the following:
1. Use the default source file name for the type of object you are creating. (To find
the default source file name for the command you are using, see
“IBM-Supplied Source Files” on page 228.)
2. Give the source member the same name as the object to be created.
For example, to create the CL program PGMA using the command defaults, you
would simply type:
CRTCLPGM PGM(PGMA)
The system would expect the source for PGMA to be in the PGMA member in the
QCLSRC source file. The library containing the QCLSRC file would be determined
by the library list.
As another example, the following Create Physical File (CRTPF) command creates
the file DSTREF using the database source file FRSOURCE. The source member is
named DSTREF. Because the SRCMBR parameter is not specified, the system
assumes that the member name, DSTREF, is the same as the name of the object
being created.
CRTPF FILE (QGPL/DSTREF) SRCFILE(QGPL/FRSOURCE)
Unnamed inline data files are files without unique file names; they are all named
QINLINE. The following is an example of an inline data file used as a source file:
//BCHJOB
CRTPF FILE(DSTPRODLB/ORD199) SRCFILE(QINLINE)
//DATA FILETYPE(*SRC)
.
If you specify a file name on the //DATA command, you must specify the same
name on the SRCFILE parameter on the CRTPF command. For example:
//BCHJOB
CRTPF FILE(DSTPRODLB/ORD199) SRCFILE(ORD199)
//DATA FILE(ORD199) FILETYPE(*SRC)
.
. (source statements)
.
//
//ENDBCHJOB
If a program uses an inline file, the system searches for the first inline file of the
specified name. If that file cannot be found, the program uses the first file that is
unnamed (QINLINE).
If a source file is a database file, you can specify a source member that contains the
needed source data. If you do not specify a source member, the source data must
be in a member that has the same name as the object being created.
The information in the object can be displayed with the Display Object Description
(DSPOBJD) command and specifying DETAIL(*SERVICE).
This information can help you in determining which source member was used and
if the existing source member was changed since the object was created.
You can also ensure that the source used to create an object is the same as the
source that is currently in the source member using the following commands:
v The Display File Description (DSPFD) command using TYPE(*MBR). This
display shows both date/times for the source member. The Last source update
date/time value should be used to compare to the Source file date/time value
displayed from the DSPOBJD command.
Note: If you are using the data written to output files to determine if the source
and object dates are the same, then you can compare the ODSRCD (source
date) and ODSRCT (source time) fields from the output file of the DSPOBJD
DETAIL(*SERVICE) command to the MBUPDD (member update date) and
MBUPDT (member update time) fields from the output file of the DSPFD
TYPE(*MBR) command.
If your source file is on a diskette, you can copy it to a database file, change it
using SEU, and copy it back to a diskette. If you do not use SEU, you have to
delete the old source file and create a new source file.
If you change a source file, the object previously created from the source file does
not match the current source. The old object must be deleted and then created
again using the changed source file. For example, if you change the source file
FRSOURCE created in “Creating an Object Using a Source File” on page 233, you
have to delete the file DSTREF that was created from the original source file, and
create it again using the new source file so that DSTREF matches the changed
FRSOURCE source file.
To assign unique sequence numbers to all the records, specify the following
parameters on the Reorganize Physical File Member (RGZPFM) command:
v KEYFILE(*NONE), so that the records are not reorganized
v SRCOPT(*SEQNBR), so that the sequence numbers are changed
v SRCSEQ with a fractional value such as .10 or .01, so that all the sequence
numbers are unique
A source file with an arrival sequence access path can be reorganized by sequence
number if a logical file for which a keyed sequence access path is specified is
created over the physical file.
Each source member description contains two date and time fields. The first
date/time field reflects changes to the member any time it is closed after being
updated.
The second date/time field reflects any changes to the member. This includes all
changes caused by SEU, commands (such as CRYF and CPYSRCF), authorization
changes, and changes to the file status. For example, the FRCRATIO parameter on
the Change Physical File (CHGPF) command changes the member status. This
date/time field is used by the Save Changed Objects (SAVCHGOBJ) command to
determine if the member should be saved. Both date/time fields can be displayed
with the Display File Description (DSPFD) command specifying TYPE(*MBR).
There are two changed date/times shown with source members:
v Last source update date/time. This value reflects any change to the source data
records in the member. When a source update occurs, the Last change
date/time value is also updated, although there may be a 1- or 2-second
difference in that date/time value.
v Last change date/time. This value reflects any changes to the member. This
includes all changes caused by SEU, commands (such as CPYF and CPYSRCF),
authorization changes, or changes to file status. For example, the FRCRATIO
parameter on the CHGPF command changes the member status, and therefore,
is reflected in the Last change date/time value.
You can create and update QTXTSRC members just like any other application
(such as QRPGSRC or QCLSRC) available with SEU. The QTXTSRC file is most
useful for narrative documentation, which can be retrieved online or printed. The
text that you put in a source member is easy to update by using the SEU add,
change, move, copy, and include operations. The entire member can be printed by
specifying Yes for the print current source file option on the exit prompt. You can
also write a program to print all or part of a source member.
“Check constraints” on page 242 provide another check for the validity of your
data by testing the data in an expression.
Primary key and unique constraints can be used as the parent key when adding a
referential constraint.
To add a physical file constraint, use the Add Physical File Constraint
(ADDPFCST) command.
v To add a unique constraint, specify a value of *UNQCST on the Type parameter.
You must also specify one or more field names for the Key parameter.
v To add a primary key constraint, specify a value of *PRIKEY on the Type
parameter. The key that you specify on the command becomes the primary
access path of the file. If the file does not have a keyed access path that can be
shared, the system creates one. You must also specify one or more field names
for the Key parameter.
v To add a check constraint, specify a value of *CHKCST on the Type parameter.
You must also specify a check constraint expression on the CHKCST parameter.
The check constraint expression has the same syntax as the expression used for
check-conditions that are defined using Structured Query Language (SQL). For
information about using SQL to set up constraints, see DB2 UDB for AS/400
SQL Reference .
You can specify any of the values below on the Constraint (CST) parameter for
each of the constraint types listed:
v CST(*ALL) to remove all of the constraints you specify on the Type parameter.
v CST(constraint-name) to remove a specific constraint.
v CST(*CHKPND) to remove only those constraints that are in check pending
status.
v Use CST(*ALL) with TYPE(*ALL) to remove all constraints from the file.
You can also use Structured Query Language (SQL) to remove a constraint. See
DB2 UDB for AS/400 SQL Reference for more information.
Note: When you remove a primary key constraint with the Structured Query
Language (SQL) ALTER TABLE statement, the inquiry message is not sent.
The key specifications are always removed and the file will have an arrival
sequence access path when the ALTER TABLE completes.
For additional details about working with a group of constraints, see “Details:
Working with a group of constraints”.
Check
Opt Constraint File Library Type State Pending
_ DEPTCST EMPDIV7 EPPROD *REFCST EST/ENB No
_ ACCTCST EMPDIV7 EPPROD *REFCST EST/ENB Yes
_ STAT84 EMPDIV7 EPPROD *REFCST DEF/ENB No
_ FENSTER REVSCHED EPPROD *REFCST EST/DSB Yes
_ IRSSTAT3 REVSCHED EPPROD *UNQCST
_ IFRNUMBERO > REVSCHED EPPROD *UNQCST
_ EVALDATE QUOTOSCHEM EPPROD *REFCST EST/ENB No
_ STKOPT CANSCRONN9 EPPROD *PRIKEY
_ CHKDEPT EMPDIV2 EPPROD *CHKCST EST/ENB No
The Work with Physical File Constraints display shows all the constraints defined
for the file specified on the WRKPFCST command. The display lists the constraint
names, the file name, and the library name. In addition, the following information
is displayed:
v The Type column identifies the constraint as referential, check, unique, or
primary key.
v The State column indicates whether the constraint is defined or established and
whether it is enabled or disabled. The State column only applies to referential
and check constraints.
v The Check Pending column contains the check pending status of the constraint.
Unique and primary key constraints do not have a state because they are always
established and enabled.
Chapter 16. Controlling the integrity of your database with constraints 239
For each of the listed constraints, you can perform the following actions:
v To change a referential or check constraint to any of its permissible states, select
Change (option 2). For example, you can enable a constraint that is currently
disabled. This option performs the same functions as the CHGPFCST command.
v To remove a constraint, select Remove (option 4). This option performs the same
functions as the RMVPFCST command.
v To display the records that are in check pending state, select Display (option 6).
This option performs the same functions as the DSPCPCST command. The
DSPCPCST command applies only to referential and check constraints.
If the constraint is not valid or if it cannot be verified, the system places it in check
pending status.
To work with the constraints that are in check pending status, perform the
following steps:
1. Make the constraint inactive. Run the Change Physical File Constraint
(CHGPFCST) command and specify *DISABLED on the Constraint state
parameter.
2. Display the list of records that are causing the constraint to be marked as check
pending. Run the Display Check Pending Constraints (DSPCPCST) command.
See “Displaying records that put a constraint in check pending status” for
additional information.
Note: The length of time that this command runs depends on the number of
records the file contains.
3. Schedule the verification of the constraints that are in check pending status.
Run the Edit Check Pending Constraints (EDTCPCST) command. See
“Processing constraints that are in check pending status” on page 241 for
additional information.
4. Make the constraint active. Run the CHGPFCST command again and specify
*ENABLED on the Constraint state parameter.
It is often useful to examine the records that do not conform to the rules of your
constraint. You can then change either the record or the constraint as necessary.
Note: Before you perform the following step, you should run the Change Physical
File Constraint (CHGPFCST) command to disable the constraint.
To display or print the list of records that have caused a constraint to be placed in
check pending status, run the Display Check Pending Constraints (DSPCPCST)
command.
To display and edit the list of constraints that are in check pending status, perform
the following steps:
1. Run the Edit Check Pending Constraints (EDTCPCST) command.
2. Check the status of the constraint you want to process.
3. If the constraint is in a status other than RUN or READY, change the *HLD
value in the Seq field to a value between 1 and 99.
4. Press Enter.
Bottom
F3=Exit F5=Refresh F12=Cancel F13=Repeat all F15=Sort by
F16=Repeat position to F17=Position to F22=Display constraint name
For additional information about processing constraints that are in check pending
status, see “Details: Processing constraints that are in check pending status”.
Details: Processing constraints that are in check pending status: The status field
of the Edit Check Pending Constraints display has one of the following values:
v RUN indicates that the constraint is being verified.
v READY indicates the constraint is ready to be verified.
v NOTVLD indicates that the access path that is associated with the constraint is
not valid. Once the access path has been rebuilt, the system automatically
verifies the constraint. This value applies only to a referential constraint.
v HELD indicates the constraint is not being verified. You must change the
sequence to a value from 1 to 99 to change this state.
v CHKPND indicates that the system attempted to verify the constraint, but the
constraint is still in check pending. You must change the sequence to a value
from 1 to 99 to change this state.
The Constraint column contains the first five characters of the constraint name. A >
symbol follows the name if it exceeds five characters. You can display the whole
long name; put the cursor on that line and press the F22 key.
Chapter 16. Controlling the integrity of your database with constraints 241
The verify time column shows the time it would take to verify the constraint if
there were no other jobs on the system. The elapsed time column indicates the
time already spent on verifying the constraint.
Unique constraints
Unique constraints act as controls in a database to ensure that rows are unique. For
example, you can specify a customer identification number as a unique constraint
in your database. If anyone attempts to create a new customer with the same
customer number, an error message is sent to the database administrator.
Unique constraints identify a field or set of fields in a database file whose values
must be unique across records in the file. The field must be in ascending order,
and can be null-capable.
A file can have multiple unique constraints, but you cannot duplicate unique
constraints. The same key fields, regardless of order, constitute a duplicate
constraint.
Unique constraints can be used as the parent key when adding a referential
constraint.
Primary key constraints identify a field or set of fields in a database file whose
values must be unique across records in the file. The field must be in ascending
order, and can be null-capable. If it is null-capable, a check constraint is implicitly
added so that null values cannot be entered in the field. You can define only one
primary key constraint for a file.
A primary key constraint can be used as the parent key when adding a referential
constraint.
Check constraints
You use check constraints to maintain limits on the values of fields so that they
conform to your database requirements.
Check constraints assure the validity of data during insertions and updates by
checking the data against a check constraint expression that you define.
For example, you can create a check constraint on a field such that values that are
inserted into that field must be between 1 and 100. If a value does not fall within
that range, the insert or update operation against your database is not processed.
Check constraints are much like referential constraints in terms of their states:
v Defined and enabled — the constraint definition has been added to the file, and
the constraint will be enforced after the constraint is established.
v Defined and disabled — the constraint definition has been added to the file, but
the constraint will not be enforced.
A check constraint, like a referential constraint, can have a check pending status. If
the data in any field violates the check constraint expression, then the constraint is
in check pending. For the insertion or update of a record, if the data violates the
check constraint expression, then the insert or update will not be allowed.
| A check constraint that contains one or more Large Object (LOB) fields is restricted
| to a narrower range of operations than a check constraint without LOB fields.
| When the check constraint includes one or more LOB fields, the LOB fields can
| only be involved in direct comparisons to:
| v Other LOB fields of the same type and same maximum length.
| v Literal values.
| v The null value.
Chapter 16. Controlling the integrity of your database with constraints 243
244 DB2 UDB for AS/400 Database Programming V4R5
Chapter 17. Ensuring data integrity with referential constraints
You can use referential constraints in AS/400 databases to enforce the referential
integrity of your system. Referential integrity encompasses all of the mechanisms
and techniques that you use to make sure that your database contains only valid
data.
For additional information about adding referential constraints, see the following
topics:
“Details: Adding a referential constraint” on page 249
“Details: Avoiding constraint cycles” on page 250
The concept of a parent key applies only in terms of a referential constraint. When
a referential constraint is added to the dependent file, a parent key is required for
the parent file. To prepare for this, you must first add either a primary key
constraint or a unqiue constraint to the parent file with the appropriate set of fields
for the key. When the referential constraint is added, a search is conducted of
unique constraints (and primary key) for a match. If a match is found, then the
access path of the constraint is used as the parent key in the referential constraint
relationship.
To create a new physical file as a parent file, perform the following steps:
1. Use the Create Physical File (CRTPF) command to create the file.
2. Use the Add Physical File Constraint (ADDPFCST) command to either add a
primary key constraint or a unique constraint. The primary key can be
null-capable, but the system creates an implicit check constraint to prevent the
insertion of null values in the field.
Note: You can use the SQL CREATE TABLE statement to perform the above steps
with one step.
To use an existing file as a parent file, choose from among the following options:
v You can add a primary key constraint to a file with the Add Physical File
Constraint (ADDPFCST) command. Specify *PRIKEY for the TYPE parameter.
You must also specify the key field or fields with the KEY parameter.
To create a dependent file, create the file as you would any physical file or use an
existing file.
The dependent file does not require a keyed access path when you create the
actual constraint. If no existing access paths meet the foreign key criteria, the
system adds an access path to the file.
To specify the rules you want to enforce with your referential constraints, perform
the following steps:
1. Run the Add Physical File Constraint (ADDPFCST) command.
2. Specify the rule that you want to enforce when you delete records (the delete
rule) by choosing a value for the DLTRULE parameter.
3. Specify the rule that you want to enforce when you update records (the update
rule) by choosing a value for the UPDRULE parameter.
Note: The system enforces a delete *RESTRICT rule immediately when the
deletion is attempted. The system enforces other constraints at the logical
end of the operation. The operation, in the case of other constraints,
includes any trigger programs that are run before or after the delete. It is
possible for a trigger program to correct a potential referential integrity
violation. For example, a trigger program could add a parent record if one
does not exist. The *RESTRICT rule does not prevent the violation.
Note: The system enforces an update *RESTRICT rule immediately when you
attempt the update. The system enforces other constraints at the logical
end of the operation. For example, a trigger program could add a parent
record if one does not exist. The *RESTRICT rule does not prevent the
violation.
If you are inserting, updating, or deleting records to a file that is associated with a
referential constraint that has a delete rule, update rule, or both rules, other than
*RESTRICT, commitment control is required. If you have not started commitment
control, the system will start and end the commit cycle automatically for you.
You can use constraint cycles in a DB2 UDB for AS/400 database; however, you
should avoid using them.
You must have a minimum of object management authority (or ALTER privilege)
to the dependent file in order to enable or disable a constraint.
Note: Users and applications must be cautious when modifying files with a
constraint relationship in the established and disabled state. Relationships
can be violated and not detected until the constraint is enabled again.
The Allocate Object (ALCOBJ) command can allocate (lock) files while a constraint
relationship is disabled. This allocation prevents others from changing the files
while this referential constraint relationship is disabled. A lock for exclusive use
allow read should be requested so that other users can read the files. Once the
constraint is enabled again, the Deallocate Object (DLCOBJ) command unlocks the
files.
When you enable or disable multiple constraints, they are processed sequentially. If
a constraint cannot be modified, you receive a diagnostic message, and the
function proceeds to the next constraint in the list. When all constraints have been
processed, you receive a completion message listing the number of constraints
modified.
When you remove a referential constraint, the system removes the associated
foreign keys and access paths from the file. The system does not remove the
foreign key access path if any logical file or other constraint on the system uses it.
If you remove a referential, primary key, or unique constraint and the associated
access path is shared by a logical file, ownership of the shared path transfers to the
logical file.
For additional information about removing referential constraints, see the following
topics:
“Details: Removing a constraint with the CST parameter” on page 252
The desired goal of this example is to ensure that every employee in the employee
file has a corresponding department that they belong to in the department file. You
can accomplish this with a referential constraint.
1. Using the ADDPFCST command, add a primary key constraint or a unique
constraint to the department file for the DEPTNO field. This will later become a
parent key. It is not yet a parent key because a referential constraint has not yet
been added.
2. Add a referential constraint to the employee file using the ADDPFCST
command. The employee file will be the dependent file. The foreign key will be
employee.DEPTNO. The department file will be the parent file with parent key
department.DEPTNO. Because there is either a primary key constraint or a
unique constraint with the DEPTNO field as the key, the constraint will serve
as the parent key associated with the referential constraint.
The referential constraint has update and delete rules that must be followed for
record inserts, updates, and deletes on the parent or dependent file.
Primary Key constraint. A field or set of fields in a database file that must be unique, ascending, and cannot
contain null values. The primary key is the primary file access path. The primary key constraint can be used as the
parent key when adding a referential constraint. A primary key constraint is really a unique constraint with some
special attributes.
Unique constraint. A field or set of fields in a database file that must be unique, ascending, and can contain null
values.
Parent Key. A field or set of fields in a database file that must be unique, ascending, and may or may not contain
null values. The parent key of the parent file is used to add a referential constraint to the dependent file. The parent
key must be either a primary key or a unique constraint.
Foreign Key. A field or set of fields in which each non-null value must match a value in the parent key of the
related parent file.
The attributes (data type, length, and so forth) must be the same as the parent key of the parent file.
Parent file. The file in a referential constraint relationship that contains the parent key.
Dependent file. The file in a referential constraint relationship that contains the foreign key. The dependent file is
dependent upon the parent file. That is, for every non-null value in the foreign key of the dependent file, there must
be a corresponding non-null value in the parent key of the parent file.
Delete rule. A definition of what action the database should take when there is an attempt to delete a parent record.
Update rule. A definition of what action the database should take when there is an attempt to update a parent
record.
The database enforces constraint rules for all I/O requests whether from
application programs or system commands (such as the INZPFM command) or
SQL statements or file I/O utilities (such as STRSEU).
The system returns a referential constraint violation if a matching parent key does
not exist for the new foreign key value, and does not insert or update the
dependent record.
For more information on the enforcement of delete and update rules, see the
following topics:
v “Enforcement of delete rules”
v “Enforcement of update rules” on page 255
If part of the delete rule enforcement fails, the entire delete operation fails and all
associated changes are rolled back. For example, a delete cascade rule causes the
database to delete ten dependent records, but a system failure occurs while
deleting the last record. The database will not allow deletion of the parent key
record, and the deleted dependent records are re-inserted.
Constraint states
A file can be in one of three constraint states. In two of the states, the constraint
can be enabled or disabled.
v Non-constraint relationship state. No referential constraint exists for a file in
this state. If a constraint relationship once existed for the file, all information
about it has been removed.
v Defined state. A constraint relationship is defined between a dependent and a
parent file. It is not necessary to create the member in either file to define a
constraint relationship. In the defined state, the constraint can be:
– Defined and enabled. A defined and enabled constraint relationship is for
definition purposes only. The rules for the constraint are not enforced. A
constraint in this state remains enabled when it goes to the established state.
– Defined and disabled. A defined constraint relationship that is disabled is for
definition purposes only. The rules for the constraint are not enforced. A
constraint in this state remains disabled when it goes to the established state.
v Established state. The dependent file has a constraint relationship with the
parent file. A constraint will be established only if the attributes match between
the foreign and parent key. Members must exist for both files. In the established
state, the constraint can be:
To get a constraint relationship out of check pending, you must disable the
relationship, correct the key (foreign, parent, or both) data, and then enable the
constraint again. The database will then verify the constraint relationship again.
When a relationship is in check pending, the parent and dependent files are in a
situation that restricts their use. The parent file I/O restrictions are different than
the dependent file restrictions. Check pending restrictions do not apply to
constraints that are in the established and disabled state (which are always in
check pending status).
For additional information about check pending status and referential constraints,
see the following topics:
“Dependent file restrictions in check pending”
“Parent file restrictions in check pending” on page 257
You can open the parent file of a constraint relationship that the system marks as
check pending, but you are limited in the types of I/O that you can do. You can
read and insert records, but you cannot delete or update records.
The order of the restore of dependent and parent files within a constraint
relationship does not matter (parent restored before dependent or dependent
restored before parent). The constraint relationship will eventually be
established.
On AS/400, you define a set of trigger actions in any supported high level
language. The following topics help you work with triggers:
v “Creating trigger programs” on page 262
v “Adding a Trigger to a File” on page 282
v “Displaying triggers” on page 283
v “Removing a trigger” on page 283
v “Triggers and their relationship to other AS/400 functions” on page 284
v “Triggers and their relationship to referential integrity” on page 285
When a user or application issues a change operation on a physical file that has an
associated trigger, the change operation calls the appropriate trigger program.
From these inputs, the trigger program can refer to a copy of the original and the
new records. You must code the trigger program so that it accepts these
parameters.
After the insertions by the application, the ATMTRANS file contains the following
data:
After being updated from the ATMTRANS file by the insert trigger program, the
ATMS file and the ACCTS file contain the following data:
After being updated from the ATMTRANS file by the update trigger programs, the
ATMS and ACCTS files contain the following data. The update to the ATMID
10002 fails because of insufficient amount in the account.
#define KEYLEN 5
/**************************************************************/
/* Need to define file structures here since there are non- */
/* character fields in each file. For each non-character */
/* field, C requires boundary alignment. Therefore, a _PACKED */
/* struct should be used in order to access the data that */
/* is passed to the trigger program. */
/* */
/**************************************************************/
/********************************************************************/
/********************************************************************/
/* Start of the Main Line Code. ************************************/
/********************************************************************/
/********************************************************************/
main(int argc, char **argv)
{
_RFILE *out1; /* file pointer for ATMS */
_RFILE *out2; /* file pointer for ACCTS */
_RIOFB_T *fb; /* file feedback pointer */
char record[16]; /* record buffer */
_FEEDBACK fc; /* feedback for message handler */
_HDLR_ENTRY hdlr = main_handler;
/********************************/
/* active exception handler */
/********************************/
/*****************************************************/
/* Open ATM and ACCTS files */
/* */
/* Check the application's commit lock level. If it */
/* runs under commitment control, then open both */
/* files with commitment control. Otherwise, open */
/* both files without commitment control. */
/*****************************************************/
if(strcmp(hstruct->commit_lock_level,"0") == 0) /* no commit */
{
if ((out1=_Ropen("APPLIB/ATMS","rr+")) == NULL)
{
printf("Error opening ATM file");
exit(1);
}
if ((out2=_Ropen("APPLIB/ACCTS","rr+")) == NULL)
{
printf("Error opening ACCTS file");
exit(1);
}
}
else /* with commitment control */
{
if ((out1=_Ropen("APPLIB/ATMS","rr+,commit=Y")) == NULL)
{
printf("Error opening ATMS file");
exit(1);
}
if ((out2=_Ropen("APPLIB/ACCTS","rr+,commit=Y")) == NULL)
{
printf("Error opening ACCTS file");
exit(1);
}
}
fb =_Rlocate(out2,&oldbuf.acctid,KEYLEN,__DFT);
if (fb->num_bytes != 1)
{
printf("record not found in ACCOUNTS\n");
_Rclose(out2);
exit(1);
}
_Rdelete(out2); /* delete record from ACCOUNTS */
_Rclose(out2);
} /* end of main */
After the deletion by the application, the ATMTRANS file contains the following
data:
After being deleted from the ATMTRANS file by the delete trigger program, the
ATMS file and the ACCTS file contain the following data:
error_code.bytes_provided = 15;
/****************************************/
/* Set the error handler to resume and */
/* mark the last escape message as */
/* handled. */
/****************************************/
*rc = CEE_HDLR_RESUME;
/****************************************/
/* Send my own *ESCAPE message. */
/****************************************/
QMHSNDPM(message_id,
&message_file,
&message_data,
message_len,
message_type,
message_q,
pgm_stack_cnt,
&message_key,
&error_code );
/****************************************/
/* Check that the call to QMHSNDPM */
/* finished correctly. */
/****************************************/
if (error_code.bytes_available != 0)
{
printf("Error in QMHOVPM : %s\n", error_code.message_id);
}
}
| /****************************************************************/
| /* INCLUDE NAME : TRGBUF */
| /* */
| /* DESCRIPTION : The input trigger buffer structure for the */
| /* user's trigger program. */
| /* */
| /* LANGUAGE : ILE C */
Offset
Dec Hex Type Field
0 0 CHAR(10) Physical file name
10 A CHAR(10) Physical file library name
20 14 CHAR(10) Physical file member name
30 1E CHAR(1) Trigger event
31 1F CHAR(1) Trigger time
32 20 CHAR(1) Commit lock level
33 21 CHAR(3) Reserved
36 24 BINARY(4) CCSID of data
40 28 BIN(4) Relative Record Number
48 30 BINARY(4) Original record offset
52 34 BINARY(4) Original record length
56 38 BINARY(4) Original record null byte map
offset
60 3C BINARY(4) Original record null byte map
length
64 40 BINARY(4) New record offset
68 44 BINARY(4) New record length
72 48 BINARY(4) New record null byte map offset
76 4C BINARY(4) New record null byte map length
80 50 CHAR(16) Reserved
* * CHAR(*) Original record
* * CHAR(*) Original record null byte map
* * CHAR(*) New record
* * CHAR(*) New record null byte map
Commit lock level. The commit lock level of the current application program. The possible values are:
’0’ *NONE
’1’ *CHG
’2’ *CS
’3’ *ALL
New record. A copy of the record that is being inserted or updated in a physical file as a result of the change
operation. The new record only applies to the insert or update operations.
New record null byte map. This structure contains the NULL value information for each field of the new record.
Each byte represents one field. The possible values for each byte are:
’0’ Not NULL
’1’ NULL
New record offset. The location of the new record. The offset value is from the beginning of the trigger buffer. This
field is not applicable if the new value of the record does not apply to the change operation, for example, a delete
operation.
New record null byte map length. The length is equal to the number of fields in the physical file.
New record null byte map offset. The location of the null byte map of the new record. The offset value is from the
beginning of the trigger buffer. This field is not applicable if the new value of the record does not apply to the
change operation, for example, a delete operation.
Original record. A copy of the original physical record before being updated or deleted. The original record only
applies to update and delete operations.
Original record null byte map. This structure contains the NULL value information for each field of the original
record. Each byte represents one field. The possible values for each byte are:
’0’ Not NULL
’1’ NULL
Original record null byte map length. The length is equal to the number of fields in the physical file.
Original record null byte map offset. The location of the null byte map of the original record. The offset value is
from the beginning of the trigger buffer. This field is not applicable if the original value of the record does not apply
to the change operation, for example, an insert operation.
Original record offset. The location of the original record. The offset value is from the beginning of the trigger
buffer. This field is not applicable if the original value of the record does not apply to the change operation; for
example, an insert operation.
Physical file library name. The name of the library in which the physical file resides.
Physical file member name. The name of the physical file member.
Physical file name. The name of the physical file being changed.
Physical file name. The name of the physical file being changed.
Relative Record Number. The relative record number of the record to be updated or deleted (*BEFORE triggers) or
the relative record number of the record which was inserted, updated, or deleted (*AFTER triggers).
Trigger time. Specifies the time, relative to the change operation on the physical file, when the trigger program is
called. The possible values are:
’1’ After the change operation
’2’ Before the change operation
Commands, statements, and operations that you cannot use in trigger programs:
Notes:
1. If the change operation is not running under commitment control, the system
always protects the change operation. However, the system does not monitor
updating the same record within the trigger program.
When the trigger program and the application program run under different
commitment definitions, the COMMIT statements in the application program only
affect its own commitment definition. The programmer must commit the changes
in the trigger program by issuing the COMMIT statement.
If the trigger program does not run under commitment control and the application
program does run under commitment control, all changes from the trigger
program are committed when either:
v A commit operation is performed in the trigger program.
v The activation group ends. In the normal case, an implicit commit is performed
when the activation group ends. However, if an abnormal system failure occurs,
a rollback is performed.
| When you associate a trigger program with a database file, you specify when the
| trigger program runs. For example, you can set up the customer order file to run a
| trigger program whenever a new record is added to the file. When the customer’s
| outstanding balance exceeds the credit limit, the trigger program can print a
| warning letter to the customer and send a message to the credit manager.
| Trigger programs are a productive way both to provide application functions and
| to manage information. Trigger programs also provide the ability for someone with
| devious intentions to create a “Trojan horse” on your system. A destructive
| program may be sitting and waiting to run when a certain event occurs in a
| database file on your system.
| Note: In history, the Trojan horse was a large hollow wooden horse that was filled
| with Greek soldiers. After the horse was introduced within the walls of Troy,
| the soldiers climbed out of the horse and fought the Trojans. In the
| computer world, a program that hides destructive functions is often called a
| Trojan horse.
| When your system ships, the ability to add a trigger program to a database file is
| restricted. If you are managing object authority carefully, the typical user will not
| have sufficient authority to add a trigger program to a database file. (Appendix D
| in the Security - Reference book tells the authority that is required or all commands,
| including the Add Physical File Trigger (ADDPFTRG) command.
| You can use the initial report as a base to evaluate any trigger programs that
| already exist on your system. Then, you can print the changed report regularly to
| see whether new trigger programs have been added to your system.
| After you have established a base of information, you can print the changed report
| regularly to monitor new trigger programs that have been added to your system.
| Figure 16 shows an example of the changed report:
|
Trigger Programs (Changed Report)
Specified library . . . . . . : LIBX
Last changed report . . . . . : 96/01/21 14:33:37
Trigger Trigger Trigger Trigger Trigger
Library File Library Program Time Event Condition
INVLIB MB108 INVPGM NEWPRICE After Delete Always
INVLIB MB110 INVPGM NEWDSCNT After Delete Always
Before you add the trigger to your physical file, you should make sure that you
have proper authority and the file has the proper data capabilities. See “Required
authorities and data capabilities for triggers” on page 283 for information about
these requirements.
You can associate a maximum of six triggers to one physical file, one trigger
during each of the following:
v Before an insert
v After an insert
v Before a delete
v After a delete
v Before an update
v After an update
Each insert, delete, or update can call a trigger before the change operation occurs
and after it occurs.
The file must have appropriate data capabilities before you add a trigger:
v CRTPF ALWUPD(*NO) conflicts with *UPDATE Trigger
v CRTPF ALWDLT(*NO) conflicts with *DELETE Trigger
Displaying triggers
The Display File Description (DSPFD) command provides a list of the triggers that
are associated with a file. Specify TYPE(*TRG) or TYPE(*ALL) to get this list. The
command provides the following information:
v The number of trigger programs
v The trigger program names and libraries
v The trigger events
v The trigger times
v The trigger update conditions
Removing a trigger
Use the Remove Physical File Trigger (RMVPFTRG) command to remove the
association of a file and trigger program. Once you remove the association, the
system takes no action when a change is made to the physical file. The trigger
program, however, remains on the system.
Copy File
v If a to-file associates with an insert trigger, each inserted record calls the trigger
program.
v If a to-file associates with a delete trigger program and the CPYF command
specifies MBROPT(*REPLACE), the copy operation fails.
v Copy with CREATE(*YES) does not propagate the trigger information
Note: If any trigger program functions do not relate to database files and cannot
be explicitly journaled, send journal entries to record relevant information.
Use the Send Journal Entry (SNDJRNE) command or the Send Journal
Entry (QJOSJRNE) API. Use this information during database file recovery
to ensure consistency.
In some cases, the system evaluates referential constraints before and after the
system calls a trigger program. This is the case with constraints that specify the
RESTRICT rule.
In some cases, all statements in the trigger program — including nested trigger
programs — run before the constraint is applied. This is true for NO ACTION,
CASCADE, SET NULL, and SET DEFAULT referential constraint rules. When you
specify these rules, the system evaluates the file’s constraints based on the nested
results of trigger programs. For example, an application inserts employee records
into an EMP file that has a constraint and trigger:
v The referential constraint specifies that the department number for an inserted
employee record to the EMP file must exist in the DEPT file.
v Whenever an insert to the EMP file occurs, the trigger program checks if the
department number exists in the DEPT file. The trigger program then adds the
number if it does not exist.
When the insertion to the EMP file occurs, the system calls the trigger program
first. If the department number does not exist in the DEPT file, the trigger program
inserts the new department number into the DEPT file. Then the system evaluates
There are some restrictions when both a trigger and referential constraint are
defined for the same physical file:
v If a delete trigger associates with a physical file, that file must not be a
dependent file in a referential constraint with a delete rule of CASCADE.
v If an update trigger associates with a physical file, no field in this physical file
can be a foreign key in a referential constraint with a delete rule of SET NULL
or SET DEFAULT.
You can use triggers to enforce referential constraints and business rules. For
example, you could use triggers to simulate the update cascade constraints on a
physical file. However, you would not have the same functional capabilities as
provided by the constraints that the system referential integrity functions define.
You may lose the following referential integrity advantages if you define them
with triggers:
v Dependent files may contain rows that violate one or more referential constraints
that put the constraint into check pending but still allow file operations.
v The ability to inform users when the system places a constraint in check
pending.
v When an application runs under COMMIT(*NONE) and an error occurs during
a cascaded delete, the database rolls back all changes.
v While saving a file that is associated with a constraint, the database network
saves all dependent files in the same library.
DB2 Multisystem allows users on distributed AS/400 systems real-time query and
update access to a distributed database as if it existed totally on their particular
system. DB2 Multisystem places new records on the appropriate system based on a
user-defined key field or fields. DB2 Multisystem chooses a system on the basis of
either a system-supplied or user-defined hashing algorithm.
:
1
When a first-changed-first-out (FCFO) access path is specified for the file, the
maximum value for the size of the key for physical and logical files is 1995
characters.
2
For files with keyed sequence access paths, the maximum number of records in a
member varies and can be estimated using the following formula:
2,867,200,000
10 + (.8 x key length)
This is an estimated value, the actual maximum number of records can vary
significantly from the number determined by this formula.
3
Both the number of bytes in a file member and the number of bytes in an access
path must be looked at when message CPF5272 is sent indicating that the
maximum system object size has been reached.
4
The maximum size of a variable-length character or DBCS field is 32,740 bytes.
DBCS-graphic field lengths are expressed in terms of characters; therefore, the
maximums are 16,383 characters (fixed length) and 16,370 characters (variable
length).
5
The maximum is 4,294,966,272 bytes if the access path is created with a maximum
size of 4 gigabytes (GB), ACCPTHSIZE(*MAX4GB).
Keep in mind that performance can suffer as you approach some of these
maximums. For example, the more logical files you have built over a physical file,
the greater the chance that system performance can suffer (if you are frequently
changing data in the physical file that causes a change in many logical file access
paths).
Normally, an AS/400 database file can grow until it reaches the maximum size
allowed on the system. The system normally will not allocate all the file space at
once. Rather, the system will occasionally allocate additional space as the file
grows larger. This method of automatic storage allocation provides the best
combination of good performance and effective auxiliary storage space
management.
If you want to control the size of the file, the storage allocation, and whether the
file should be connected to auxiliary storage, you can use the SIZE, ALLOCATE,
and CONTIG parameters on the Create Physical File (CRTPF) and the Create
Source Physical File (CRTSRCPF) commands.
You can use the following formulas to estimate the disk size of your physical and
logical files.
v For a physical file (excluding the access path):
The size of the physical file depends on the SIZE and ALLOCATE parameters on
the CRTPF and CRTSRCPF commands. If you specify ALLOCATE(*YES), the
initial allocation and increment size on the SIZE keyword must be used instead
of the number of records.
v For a logical file (excluding the access path):
This equation is used for both three and four byte indexes by changing the set of
constants in the equation as follows:
This should reduce everything needed to calculate the index size to the type of
index (i.e. 3 or 4 byte), the total key size, and the number of keys. The estimate
should be greater than the actual index size because the common text estimate is
minimal.
Given this generalized equation for index size, the LogicalPageSize is as follows:
Table 12. LogicalPageSize Values
*MAX4GB (3-byte) *MAX1TB (4-byte)
Key Length LogicalPageSize LogicalPageSize
1 - 500 4096 bytes 8192 bytes
501 - 1000 8192 bytes 16384 bytes
1001 - 2000 16384 bytes 32768 bytes
CommonTextPerKey = 0
TerminalTextPerKey = KeySizeInBytes
c = 0
This estimate can differ significantly from your file. The keyed sequence access
path depends heavily on the data in your records. The only way to get an accurate
size is to load your data and display the file description.
Note: Additional space is not required for an arrival sequence access path.
In addition to the file sizes, the system maintains internal formats and directories
for database files. (These internal objects are owned by user profile QDBSHR.) The
following are estimates of the sizes of those objects:
v For any file not sharing another file’s format:
This appendix describes DBCS considerations as they apply to the database on the
AS/400 system.
The specific DBCS data types (specified in position 35 on the DDS coding form.)
are:
Entry Meaning
O DBCS-open: A character string that contains both single-byte and bracketed
double-byte data.
E DBCS-either: A character string that contains either all single-byte data or
all bracketed double-byte data.
J DBCS-only: A character string that contains only bracketed double-byte
data.
G DBCS-graphic: A character string that contains only nonbracketed
double-byte data.
Note: Files containing DBCS data types can be created on a single-byte character
set (SBCS) system. Files containing DBCS data types can be opened and
used on a SBCS system, however, coded character set identifier (CCSID)
conversion errors can occur when the system tries to convert from a DBCS
or mixed CCSID to a SBCS CCSID. These errors will not occur if the job
CCSID is 65535.
DBCS Constants
A constant identifies the actual character string to be used. The character string is
enclosed in apostrophes and a string of DBCS characters is surrounded by the
DBCS shift-out and shift-in characters (represented by the characters < and > in the
following examples). A DBCS-graphic constant is preceded by the character G. The
types of DBCS constants are:
Type Example
When using the wildcard function with a bracketed-DBCS field, both single-byte
and double-byte wildcard values (asterisk and underline) are allowed. The
following special rules apply:
v A single-byte underline refers to one EBCDIC character; a double-byte underline
refers to one double-byte character.
v A single- or double-byte asterisk refers to any number of characters of any type.
When using the wildcard function with a DBCS-graphic field, only double-byte
wildcard values (asterisk and underline) are allowed. The following special rules
apply:
v A double-byte underline refers to one double-byte character.
v A double-byte asterisk refers to any number of double-byte characters.
:
1
For save-while-active, the file lock is *SHRUPD initially, and then the lock is reduced to *SHRRD. See the
Backup and Recovery for a description of save-while-active locks for the save commands.
2
For save-while-active, the member/data lock is *SHRRD.
3
The clear does not happen if the member is open in this process or any other process.
:
1
Exclusive lock (*EXCL). The object is allocated for the exclusive use of the
requesting job; no other job can use the object.
2
Exclusive lock, allow read (*EXCLRD). The object is allocated to the job that
requested it, but other jobs can read the object.
3
Shared lock, allow read and update (*SHRUPD). The object can be shared either
for read or change with other jobs.
4
Shared lock, read only (*SHRNUPD). The object can be shared for read with other
jobs.
5
Shared lock (*SHRRD). The object can be shared with another job if the job does
not request exclusive use of the object.
Index 309
database file (continued) date (continued) determining
handling errors in a program 191 comparison using OPNQRYF data sharing requirements 107
joining without DDS 143 command 161 duplicate key values 105
locking duration 162 existing record formats 12
considerations 303 DB2 Multisystem 287 field-level security requirements 87
wait time 108 DBCS (double-byte character set) if multiple record types are needed in
minimum size 295 considerations 297 files 41
naming 102 constant 297 security requirements 87
opening field when a source statement was
commands to use 123 comparing 299, 301 changed 236
members 123 concatenating 298 which source file member was used to
sequential-only processing 116 concatenation function 301 create an object 234
shared in a job 109 data types 297 device source file
shared in an activation group 109 mapping 298 using 230
override 30, 102 substring 299 DFT (Default) keyword 12, 42
processing options 102 using the concatenation dictionary-described data
protecting function 301 definition 4
commitment control 105 wildcard function 300 Display Database Relations (DSPDBR)
journaling 105 DDM (distributed data command 16, 208
recovering data 215 management) 158 Display File Description (DSPFD)
setting a position 175 DDS (data description specifications) command
setting up 3 describing output file 212
sharing across jobs 107 database file 7 relating source and objects 234
sharing in a job logical file, example 10 Display File Field Description (DSPFFD)
close 111 physical file, example 7 command 42, 208
input/output considerations 111 using, reasons 5 Display Journal (DSPJRN) command
open 109 Default (DFT) keyword 12, 42 output files 212
open data path defining Display Message Descriptions
considerations 171 fields 149 (DSPMSGD) command 191
SHARE parameter 32, 108 delaying Display Object Description (DSPOBJD)
sharing in an activation group end-of-file processing 104 command 234
close 111 Deleted Percentage (DLTPCT) Display Physical File Member (DSPPFM)
input/output considerations 111 parameter 37 command 18, 199
open 109 deleted record Display Problem (DSPPRB)
SHARE parameter 108 reusing 103 command 212
sizes deleting Display Program References
maximum 291 authority 88 (DSPPGMREF) command 209
minimum 295 database record 37, 186 Display Record Locks (DSPRCDLCK)
specifying deriving new fields from existing command 107
system where created 33 fields 42 displaying
wait time for locked 33 DESCEND (Descend) keyword 21 attributes of files 207
types 28 descending sequence database relations 16, 208
with different record formats 127 arranging key fields 20 descriptions of fields in a file 208
writing the output from a command describing errors 191
to 211
access paths file description 212, 234
database member for database files 17 file field description 42, 208
adding to files 195 for logical files 45 files used by programs 209
attributes 27 overview 7 information about database files 207
managing 195 data to the system 5 journal 212
naming 102 database file message description 191
number allowed 28 to the system 6 object description 234
removing 196 with DDS 7 physical file member 18, 199
database record logical file physical file member records 199
field use 41 problem 212
adding 183
deleting 186 floating-point fields in 45 program reference 209
record format 39 record lock 107
file attributes 27
with DDS, example 10 relationships between files on the
reading methods
arrival sequence access path 176 physical files with DDS system 208
example 7 system cross-reference files 210
keyed sequence access path 177
updating 182 record format 6 triggers 283
description distributed data management
DataLinks
checking for changes to the record (DDM) 158
See DB2 UDB for AS/400 SQL format 29 distribution, database 287
Programming 3 sharing existing record format 15 divide by zero
date using existing field 12 handling 151
arithmetic using OPNQRYF designing DLTPCT (Deleted Percentage)
command 163 additional named fields 39 parameter 37
Index 311
field (continued) FMTSLR (Format Selector) Initialize Physical File Member (INZPFM)
using parameter 185 command 186, 196
data dictionary for reference 15 Force Access Path (FRCACCPTH) initializing
existing descriptions and reference parameter 29, 106 data in a physical file member 196
files 12 Force-End-Of-Data (FEOD) input-only field 42
floating point in access paths 25 operation 185 input/output
logical files to describe 41 Force-Write Ratio (FRCRATIO) parameter blocked 115
multiple key 21 data integrity considerations 106 sequential-only processing 117
field definition specifying file and member sharing files in a job 111
derived from existing field attributes 29 sharing files in an activation
definitions 149 FORMAT (Format) keyword 16 group 111
functions 11 FORMAT (Format) parameter interactive data definition utility
field reference file OPNQRYF (Open Query File) (IDDU) 5
command 142 INZPFM (Initialize Physical File Member)
definition 12
format, record command 186, 196
FIFO (First-In First-Out) keyword 23
logical file, describing 39
file
FORMAT parameter
closing database
sequential-only processing 118
creating a file, considerations 157 J
Format Selector (FMTSLR) JDFTVAL (Join Default Values)
shared in a job 111 parameter 185 keyword 65
shared in an activation group 111 FRCACCPTH (Force Access Path) JDUPSEQ (Join Duplicate Sequence)
copy 27 parameter 29, 106 keyword 68
copying FRCRATIO (Force-Write Ratio) JFILE (Joined Files) keyword 39
adding members 195 parameter 29, 106 Join Default Values (JDFTVAL)
copying to and from files 231
keyword 65
processing keyed sequence
files 18 G Join Duplicate Sequence (JDUPSEQ)
keyword 68
writing data to and from source Grant Object Authority (GRTOBJAUT)
command 90 join field
file members 230
graphic-DBCS constant 297 definition 62
creating physical 35
graphic-DBCS data 297 rules to remember 85
creating source 227
Group Select (GRPSLT) keyword 155 join logical file
database
grouping complex, example 81
attributes 27
data from database file records 152 considerations 60
closing 189
performance 169 definition 61
options for processing 102
GRPSLT (Group Select) keyword 155 example 81
processing options 102
GRTOBJAUT (Grant Object Authority) field 85
describing database
command 90 matching records, case 65
to the system 6
reading 63
with DDS 7
requirements 84
in a job 171
logical
H setting up 68
high-level language (HLL) program specifying select/omit statements 76
creating 53
writing considerations 158 summary of rules 84
describing record format 39
HLL (high-level language) program Join Order (JORDER) parameter 143
setting up 68
writing considerations 158 Joined Files (JFILE) keyword 39
naming 27
joining
opening 123
database files without DDS 143
physical
creating 35 I performance 169
physical file to itself, example 78
specifying attributes 35 IBM-supplied source file 228
three or more physical files,
sharing IDDU (interactive data definition
example 76
database, across jobs 107 utility) 5
two physical files 60
database, in the same activation ignoring
two physical files, example 61
group 108 keyed sequence access path 104
JORDER (Join Order) parameter 143
database, in the same job 32, 108 record format 105
journaling
source 28 implicit access path sharing 52
commitment control 105, 216
specifying improving
physical file 105
member 33 performance
text 33 for sort sequence 169
FILE (File) parameter 102 suggestions 83
FILE parameter 27 with OPNQRYF command and K
FILETYPE (File Type) parameter 28 keyed sequence access path 166 keeping
index 7 access paths current 30
final total-only processing 154
adding key field
First-Changed First-Out (FCFO)
in Operations Navigator 95 arranging
keyword 23
Inhibit Write (INHWRT) parameter 106 ascending sequence 18, 20
First-In First-Out (FIFO) keyword 23 INHWRT (Inhibit Write) parameter 106 changing order 18
floating point field initial file position changing order with SRTSEQ 19
use in access paths 25 specifying 103 descending sequence 18, 20
Index 313
object (continued) Operations Navigator (continued) parameter (continued)
creating from source statement in a view EXPDATE (Expiration Date)
batch job 233 creating 94 (continued)
move 27 OPNDBF (Open Database File) specifying expiration date 35, 106
object authority command 123 FILE 27, 102
editing 90 OPNID (Open File Identifier) FILETYPE (File Type) 28
granting 90 parameter 124 FMTSLR (Format Selector) 185
revoking 90 OPNQRYF (Open Query File) command FORMAT 142, 157
omitting records using logical files 46 running, messages sent 158 FRCACCPTH (Force Access Path) 29,
Open Database File (OPNDBF) using 106
command 123 copying 172 FRCRATIO (Force-Write Ratio)
Open File Identifier (OPNID) date, time, and timestamp data integrity considerations 106
parameter 124 arithmetic 161 specifying file and member
Open Query File (OPNQRYF) command date, time, and timestamp attributes 29
running, messages sent 158 comparison 161 INHWRT (Inhibit Write) 106
using DBCS fields 300 JORDER (Join Order) 143
copying 172 for more than just input 160 KEYFILE 197
date, time, and timestamp for random processing 166 KEYFLD (Key Field) 154
arithmetic 161 results of a query 155 LANGID (Language Identifier) 34
date, time, and timestamp selecting records, examples 131 LVLCHK (Level Check) 29, 106
comparison 161 to select/omit records 50 MAINT (Maintenance) 30
DBCS fields 300 typical errors 173 MAPFLD (Mapped Field) 145
for more than just input 160 OPNSCOPE (Open Scope) MAXMBRS (Maximum Number of
for random processing 166 parameter 124 Members) 28
results of a query 155 option MBR (Member)
selecting records, examples 131 database file processing 102 opening members 124
to select/omit records 50 OPTION parameter 102, 123 processing data 102
typical errors 173 OUTFILE parameter 211 specifying member names 27
Open Scope (OPNSCOPE) output file NBRRCDS (Number Of Records
parameter 124 Display File Description (DSPFD) Retrieved At Once) 115
opening command 212 OPNID (Open File Identifier) 124
database file Display Journal (DSPJRN) OPNSCOPE (Open Scope) 124
commands to use 123 command 212 OPTION 102, 123
members 123 Display Problem (DSPPRB) OUTFILE 211
sequential-only processing 116 command 212 POSITION 103, 175
shared in a job 109 for CL commands 211 QRYSLT (Query Select) 50
shared in an activation group 109 Override with Database File (OVRDBF) RCDFMT (Record Format) 16
query file 123, 124 command 30, 101 RCDFMTLCK (Record Format
overview Lock) 108
operation
Operations Navigator RCDLEN (Record Length) 5, 37
basic database file 175
database 93 RECORDS 196
physical file member 196
OVRDBF (Override with Database File) RECOVER 31
operations
command 30, 101 REUSEDLT (Reuse Deleted
database 93
Records) 37
Operations Navigator
alias P SEQONLY (Sequential-Only
Processing) 115, 124
creating 95 parameter SHARE
database 93 ACCPTH (Access Path) 104, 124 changing for logical files 195
accessing 93 ALLOCATE (Allocate) 36 improving performance 32, 108
overview 93 ALWDLT (Allow Delete) 38, 90 SIZE 36
index ALWUPD (Allow Update) 38, 90 SRCFILE (Source File) 28
adding 95 AUT (Authority) 33, 90 SRCMBR (Source Member) 28
library CCSID (Coded Character Set SRCOPT (Source Update
creating 94 Identifier) 33 Options) 199, 231
SQL function COMMIT 105, 124 SRCSEQ (Source Sequence
creating 96 CONTIG (Contiguous Storage) 36 Numbering) 231
SQL performance monitor DLTPCT (Deleted Percentage) 37 SRCTYPE (Source Type)
creating 97 DTAMBRS (Data Members) specifying source type of a
SQL procedure selecting 58 member 38
creating 96 specifying read order 23, 27 SRTSEQ (Sort Sequence) 33
SQL scripts DUPKEYCHK (Duplicate Key SYSTEM 33
creating 96 Check) 105, 124 TEXT 33, 195
table EOFDLY (EOF Retry Delay) 104 TYPE 124
creating 94 EXPCHK (Check Expiration UNIT 28
tasks Date) 106 WAITFILE 33, 108
database 93 EXPDATE (Expiration Date) WAITRCD (Wait Record) 33, 107
type changing of physical file
creating 96 member 195
Index 315
relationships selecting (continued) source file (continued)
record format 17 record (continued) statements, determining when
releasing without using DDS, example 130 changed 236
locked records 182 selection supplied by IBM 228
Remove Member (RMVM) performance 169 using
command 196 SEQONLY (Sequential-Only Processing) device 230
removing parameter 115, 124 for documentation 236
members from files 196 sequence access path in a program 232
trigger 283 arrival 17 Source File (SRCFILE) parameter 28
RENAME (Rname) keyword 44 keyed 18 source file member
RENAME keyword 39 sequential-only processing 115 determining which used to create an
Rename Member (RNMM) close considerations 118 object 234
command 196 input/output considerations 117 reorganizing data 235
renaming open considerations 116 Source Member (SRCMBR)
field 44 SEQONLY parameter 115, 124 parameter 28
member 196 Sequential-Only Processing (SEQONLY) source physical file
Reorganize Physical File Member parameter 115, 124 creating
(RGZPFM) command 186, 197 RCDLEN parameter 5
setting position in file 175
reorganizing source files 227
setting up
data in physical file members 186, using, example 35
database file 3
197 Source Sequence Numbering (SRCSEQ)
join logical file 68
source file member data 235 parameter 231
logical file 39
table 197 source type
physical file 35
restoring specifying 38
SEU (source entry utility) 230
database 215 Source Type (SRCTYPE) parameter
Retrieve Member Description SHARE (Share) parameter
changing for logical files 195 specifying 38
(RTVMBRD) command 207
improving performance 32, 108 Source Update Options (SRCOPT)
retrieving
sharing parameter 199, 231
member description 208
access path 179 specifications
records in a multiple format file 56
file using existing access path 25
Reuse Deleted Records (REUSEDLT)
across jobs 107 specifying
parameter 37
REUSEDLT (Reuse Deleted Records) in the same activation group 108 access path maintenance levels 30
parameter 37 in the same job 32, 108 attributes
Revoke Object Authority (RVKOBJAUT) OPNQRYF command 171 physical file and member 35
command 90 implicit access path 52 authority 87
RGZPFM (Reorganize Physical File record format descriptions that database
Member) command 186, 197 exist 15 file text 33
RMVM (Remove Member) sharing limitation member text 33
command 196 record format 17 delayed maintenance, access path 30
RNMM (Rename Member) SIGNED (Signed) keyword 25 expiration date of a file 35, 106
command 196 file text 33
SIZE parameter 36
RTVMBRD (Retrieve Member how a file is shared 107
sort sequence
Description) command 207 immediate maintenance, access
performance considerations 169 path 30
run time specifying 33
considerations 101, 172 initial file position 103
Sort Sequence (SRTSEQ) parameter 33 key field
summary 118
source entry utility (SEU) 230 from different files 143
RVKOBJAUT (Revoke Object Authority)
source file in join logical files, example 75
command 90
attributes keyed sequence access path without
changing 235 DDS 142
S types 228 LANGID (Language Identifier) 34
secondary file concepts 227 language identifier 34
definition 62 copying data 230 maximum number of members 28
example 66 creating maximum size of a file 291
handling missing records in join 147 commands 227 member attributes 35
using default data for missing object 233 member text 33
records 79 with DDS 28, 229 members, physical files 35
security without DDS 229 physical file and member
database 87 entering data 230 attributes 35
specifying authority 33, 87 importing from non-AS/400 physical file attributes 35
select/omit system 232 public authority 33
access path 50 loading from non-AS/400 rebuild maintenance, access path 30
dynamic 50 system 232 record length 37, 104
selecting maintaining data 230 select/omit statements in join logical
record managing 235 files 76
using logical files 46 sequence numbers used in sort sequence 33
using OPNQRYF command 131 copies 231 source type of a member 38
Index 317
318 DB2 UDB for AS/400 Database Programming V4R5
Printed in U.S.A.