Wednesday, November 19, 2025

OPNQRYF (Open Query File) Command Description

OPNQRYF Command Overview

Purpose

The Open Query File (OPNQRYF) command is used to open a file that contains records meeting specific query conditions. Once opened, the file behaves like one opened with the OPNDBF command, and programs can access its records through a shared open data path (ODP). When finished, the path and resources are released with the CLOF command.

This command can perform several database operations:

  • Join records from multiple files, members, or formats (supports both equal and non-equal joins).
  • Compute new values using arithmetic or character expressions.
  • Group records by field values and apply aggregate functions (e.g., MIN, AVG).
  • Filter records before or after grouping.
  • Sort results by one or more key fields.

Restrictions

  1. Overrides can change file, library, or member names on the FILE parameter, but not on the FORMAT parameter unless FORMAT(*FILE) is specified.
  2. OPNQRYF does not share an existing ODP. If a shared ODP already exists with the same file/member, the command fails.
  3. Subsequent shared opens must use the same options as the initial OPNQRYF.
  4. Some system commands (e.g., DSPPFM, CPYF) do not share ODPs, so OPNQRYF cannot be used with them.
  5. BASIC programs cannot use OPNQRYF because they don’t share ODPs.
  6. In multithreaded jobs, OPNQRYF is not threadsafe for distributed files using *SNA.
  7. Required authorities include:
    • Execute authority for libraries.
    • Operational and data authorities (read, add, update, delete) depending on the file access mode.
    • Operational authority for FORMAT files.
    • Use authority for translation tables on MAPFLD.

Key Parameters

FILE

Defines the files, members, and record formats to be processed.

  • Supports physical, logical, or DDM files.
  • Library qualifiers: *LIBL, *CURLIB, or explicit library name.
  • Member options: *FIRST, *LAST, or specific member.
  • Record format options: *ONLY or a named format.
  • Up to 32 physical file members can be joined in one query.

OPTION

Specifies how the file is opened:

  • *INP → Input only
  • *OUT → Output
  • *UPD → Update
  • *DLT → Delete
  • *ALL → All operations

FORMAT

Defines the record format for the query results.

  • Can be mapped fields or unique field names.
  • FORMAT(*FILE) uses the format of the first file listed.

QRYSLT

Sets selection criteria before grouping.

  • *ALL → Selects all records.
  • Expression → Logical conditions (e.g., CUSNBR<7000 *AND BALDUE>CRLIMIT*0.9).

KEYFLD

Determines record ordering.

  • *NONE → No specific order.
  • *FILE → Order follows the first file’s access path.
  • Qualified field names → Up to 50 fields, with ascending/descending options.

UNIQUEKEY

Controls uniqueness of key fields.

  • *NONE → No uniqueness required.
  • *ALL → All key fields must be unique.
  • Number → Specifies how many key fields must be unique.

JFLD

Defines join conditions between files.

  • Specifies pairs of fields for join operations.
  • Supports operators like *EQ, *NE, *GT, *LT, *GE, *LE.

JDFTVAL

Controls whether default values are used when join matches are missing.

  • *NO → No defaults.
  • *YES → Include records with defaults.
  • *ONLYDFT → Only include records created with defaults.

JORDER

Specifies join order.

  • *ANY → System chooses order for performance.
  • *FILE → Order follows FILE parameter.

GRPFLD

Defines grouping fields.

  • *NONE → No grouping.
  • Qualified field names → Up to 50 fields.

GRPSLT

Applies selection after grouping.

  • *ALL → All groups included.
  • Expression → Logical conditions using grouping fields or aggregate functions.

MAPFLD

Defines mapped or derived fields.

  • *NONE → No mapped fields.
  • Named fields → Used in other parameters to reference derived values.


MODULE

Understanding ILE Module Objects

What is a Module?

  • A module is a non-executable object created by an ILE compiler.
  • The system identifies it with the special symbol *MODULE.
  • Modules act as building blocks in larger applications, allowing developers to reuse code efficiently.

Why Use Modules?

  • They simplify development in large systems by enabling code reuse.
  • A module can contain one or more procedures (similar to functions).
  • Procedures accept parameters, perform logic, and can return values.
  • Modules can be combined into different executable objects, making them versatile components.

Creating Programs and Service Programs

  • When you compile source code with an ILE compiler, you get a *MODULE.
  • You can then use:
    • CRTPGM → to build a *PGM (program object)
    • CRTSRVPGM → to build a *SRVPGM (service program object)
  • During this process, the compiled code is copied from the module into the program or service program.
  • Once copied, the module itself is no longer required unless you plan to rebuild the program later.
  • Important: You cannot directly call a module; only the routines inside it are callable.

Procedures in Modules

  • In ILE terminology, routines inside modules are called procedures.
  • There are two types:
  • Both are invoked in the same way, but the naming convention sometimes creates confusion, leading people to think they are calling the module directly.

Key Points About Modules

  • Modules group related tasks into smaller, manageable units.
  • They can be bound into programs or service programs.
  • They cannot be invoked with a dynamic CALL. Instead, they are accessed through:
    • CALLB (bound call in RPG)
    • CALLP (prototyped call in RPG)
  • NOMAIN modules cannot be called directly; only their subprocedures can be invoked.

Types of RPG Modules

1. Cycle-Main Module

  • Contains a cycle-main procedure plus optional subprocedures.
  • Implements the full RPG cycle logic.
  • Can be called via a bound call or a program call.

2. Linear-Main Module

  • Contains a linear-main procedure and optional subprocedures.
  • The main procedure is explicitly defined with the MAIN keyword.
  • Can only be called through a program call (not a bound call).

3. NOMAIN Module

  • Defined with the NOMAIN keyword.
  • Contains only subprocedures, with no main procedure.
  • Cannot serve as the entry point of a program.

RPG Module Types Summary

Module TypeKeywordRPG Cycle FeaturesMain ProcedureBehavior
Cycle-mainYesImplicitly defined in sourceRuns RPG cycle logic; can be called via bound or program call
Linear-mainMAINNoExplicitly defined with MAINEntry procedure only via program call
NOMAINNOMAINNoNoneContains only subprocedures; cannot be program entry

Linear-Main Modules Explained

  • A linear-main module is created by specifying the MAIN keyword in the control specification.
  • It has one or more procedures, with one designated as the entry point.
  • Unlike cycle-main modules, it does not include RPG cycle-related specifications.

Rules for Linear-Main Procedures

  • The procedure name must match one defined in the source.
  • Calls to the linear-main procedure are program calls, not bound calls.
  • Recursive calls are treated as dynamic program calls.
  • If a prototype is defined, it must include the EXTPGM keyword.
  • Exporting the main procedure is not allowed (EXPORT keyword cannot be used).

Linear and NOMAIN Modules

  • Modules compiled with MAIN or NOMAIN keywords exclude the RPG program cycle.
  • Restrictions in the main source section:
    • No primary/secondary file specs
    • No heading/detail/total output specs
    • No executable calculations (including *INZSR)
    • No *ENTRY PLIST
  • Allowed elements include:
    • Full-procedural files
    • Input and definition specifications
    • Declarative calculations (DEFINE, KFLD, KLIST, PARM, PLIST)
    • Exception output

⚠️ Note: In linear modules, global files and data areas are not automatically closed or unlocked. Developers must explicitly handle these operations.



Thursday, January 11, 2024

Set screen field attribute using P Field


DSPATR(&program-to-system-field)

The program-to-system-field parameter is required and specifies that the named field must be defined in the record format, alphanumeric (A in position 35), length of one, and usage P (P in position 38). The program uses this P-field to set the display attribute for the field this DSPATR keyword applies to.The name P-field is used for multiple fields with the record being defined. One DSPATR P-field is allowed per field. The P-field contains the display attribute and identifies whether the field should be protected.

Valid P-field values

The DSPATR P-field does not support the following display attributes:

  • MDT - Set changed data tag when displayed
  • OID   - Operator identification
  • PC     - Position cursor
  • SP      - Select by light pen

Valid P-field values (nonprotect)

Hex

Limited color

Full color

20

Normal

Green

21

Reverse image

Green, reverse image

22

High intensity

White

23

High intensity, reverse image

White, reverse image

24

Underscore

Green, underscore

25

Underscore, reverse image

Green, underscore, reverse image

26

Underscore, high intensity

White, underscore

27

Nondisplay

Nondisplay

28

Blink

Red

29

Blink, reverse image

Red, reverse image

2A

Blink, high intensity

Red, high intensity

2B

Blink, high intensity, reverse image

Red, high intensity, reverse image

2C

Blink, underscore

Red, underscore

2D

Blink, underscore, reverse image

Red, underscore, reverse image

2E

Blink, underscore, high intensity

Red, underscore, blink

2F

Nondisplay

Nondisplay

30

Column separator

Turquoise, column separator

31

Reverse image, column separator

Turquoise, column separator, reverse image

32

High intensity, column separator

Yellow, column separator

33

High intensity, reverse image, column separator

White, reverse image, column separator

34

Underscore, column separator

Turquoise, underscore, column separator

35

Underscore, reverse image, column separator

Turquoise, underscore, reverse image, column separator

36

Underscore, high intensity, column separator

Yellow, underscore, column separator

37

Nondisplay

Nondisplay

38

Blink, column separator

Pink

39

Blink, reverse image, column separator

Pink, reverse image

3A

Blink, high intensity, column separator

Blue

3B

Blink, high intensity, reverse image, column separator

Blue, reverse image

3C

Blink, underscore, column separator

Pink, underscore

3D

Blink, underscore, reverse image, column separator

Pink, underscore, reverse image

3E

Blink, underscore, high intensity, column separator

Blue, underscore

3F

Nondisplay

Nondisplay    


Valid P-field values (protect)

Hex

Limited color

Full color

A0

Normal

Green

A1

Reverse image

Green, reverse image

A2

High intensity

White

A3

High intensity, reverse image

White, reverse image

A4

Underscore

Green, underscore

A5

Underscore, reverse image

Green, underscore, reverse image

A6

Underscore, high intensity

White, underscore

A7

Nondisplay

Nondisplay

A8

Blink

Red

A9

Blink, reverse image

Red, reverse image

AA

Blink, high intensity

Red, high intensity

AB

Blink, high intensity, reverse image

Red, high intensity, reverse image

AC

Blink, underscore

Red, underscore

AD

Blink, underscore, reverse image

Red, underscore, reverse image

AE

Blink, underscore, high intensity

Red, underscore, blink

AF

Nondisplay

Nondisplay

B0

Column separator

Turquoise, column separator

B1

Reverse image, column separator

Turquoise, column separator, reverse image

B2

High intensity, column separator

Yellow, column separator

B3

High intensity, reverse image, column separator

White, reverse image, column separator

B4

Underscore, column separator

Turquoise, underscore, column separator

B5

Underscore, reverse image, column separator

Turquoise, underscore, reverse image, column separator

B6

Underscore, high intensity, column separator

Yellow, underscore, column separator

B7

Nondisplay

Nondisplay

B8

Blink, column separator

Pink

B9

Blink, reverse image, column separator

Pink, reverse image

BA

Blink, high intensity, column separator

Blue

BB

Blink, high intensity, reverse image, column separator

Blue, reverse image

BC

Blink, underscore, column separator

Pink, underscore

BD

Blink, underscore, reverse image, column separator

Pink, underscore, reverse image

BE

Blink, underscore, high intensity, column separator

Blue, underscore

BF

Nondisplay

Nondisplay


In DDSSRC:
A* First declare the variable 
 A                PFLD1           1A  P
 A*
 A* then, after the display field in function limit you assign it
 A*
 A                FIELD01       14Y 2B 17 42EDTCDE(1)    
 A                                          DSPATR(&PFLD1)

In RPGSRC, just set or clear when you need it:
C* 
C* Set the hex value in the QDDSSRC variable
C                       EVAL      PFLD1 = x'A0'
C*
C* Clear the variable
C                   CLEAR                   PFLD1              1

The following example shows how to specify the DSPATR keyword with P-field usage:
|...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
     A          R RECORD                   
     A            FLD1           5A     2  6DSPATR(&PFLD1)
     A            FLD2           5A     2  6DSPATR(&PFLD2)
     A            PFLD1          1A  P
     A            PFLD2          1A  P
     A

There is another technique which might interest you, and that is the DDS keyword DSPATR() used with a program to system field.  Instead of communicating with the display file via indicators, you can set the attributes .

Sample source code:

A                                      CF03(03 'Exit')
A*
A          R DSPFR                     TEXT('Display file examples')
A                                      BLINK
A*
A                                  9  2'Some input'
A                                      DSPATR(HI UL)
A            FLDA          10   B 10  2TEXT('Generic input/output')
A                                      DSPATR(&FLDAATR)
A            FLDB          20   B 11  2TEXT('Generic input/output')
A                                      DSPATR(&FLDBATR)
A            FLDAATR        1   P
A            FLDBATR        1   P

*****************************************************
H debug option(*srcstmt: *nodebugio)
H actgrp('QILE') dftactgrp(*no)
 * dbgview(*list)

 * Show use of program to system attribute field

fdspfatr   cf   e             workstn

 * work variables
d atr             s                   like(fldaatr)

 * field attribute bit patterns
d NO              s              1a   inz(x'00')
no attrs
d RI              s              1a   inz(x'01')
reverse
d HI              s              1a   inz(x'02')
highlight
d UL              s              1a   inz(x'04')
underline
d ND              s              1a   inz(x'07')
non display
d BL              s              1a   inz(x'08')
blink
d NP              s              1a   inz(x'20')
non protect
d CS              s              1a   inz(x'30')
column sep
d PR              s              1a   inz(x'80')
protect

d setAtr          pr                  like(atr)
d  inpFld                       50    const

c                   dow       *in03 = *off
c                   exfmt     dspfr
c   03              leave
c                   exsr      process
c                   enddo

c                   eval      *inLR = *On

 * ===========================================================
c     process   begsr

 * Top field to specify the colour of the bottom field, vice versa
c                   eval      FLDAATR = setAtr(FLDB)
c                   eval      FLDBATR = setAtr(FLDA)
c                   endsr

 * ===========================================================
p setAtr          b
d setAtr          pi                  like(atr)
d  inpFld                       50    const

d Wrk_fld         s                   like(atr)

 * reset all attributes
c                   eval      Wrk_fld         = NP

c                   select
c                   when      inpFld = 'RI'
c                   biton     RI            Wrk_fld         
c                   when      inpFld = 'HI'
c                   biton     HI            Wrk_fld         
c                   when      inpFld = 'UL'
c                   biton     UL            Wrk_fld         
c                   when      inpFld = 'BL'
c                   biton     BL            Wrk_fld         
c                   when      inpFld = 'CS'
c                   biton     CS            Wrk_fld         
c                   endsl

c                   return    Wrk_fld         

 p                 e