If you're looking to pass parameters to a query (and who isn't?), query management queries may be just the thing for you. For example, it's a snap to pass record selection criteria to a query management query. Consider the following query management query:
-- * ===================================================================
-- * = QMQry......... Employees2 =
-- * = Description... Sample QMQry =
-- * ===================================================================
Select *
From YourLib/Employees
Where Location = &INLOC
and Department = &INDEPT
Notice that this query is identical to the first example except in the Where clause, which now specifies variables &INLOC and &INDEPT as record selection criteria. You pass values for these variables to the query management query at run time.
Enter the source for QMQry Employees2 and compile it as you did with Employees1 earlier. Then, from the command line, run it with the following command:
StrQMQry QMQry(YourLib/Employees2)
You'll now see the Display Program Messages panel asking you to enter a value for field INLOC.
******************************************************************************** * Display Program Messages * * * * Job 361742/GGUTHRIE/SNBGARYA1 started on 08/14/02 at 07:40:11 in subsystem Q * * Type a value for variable "INLOC" and press Enter. * * * * * * * * * * * * * * * * * * * * * * * * * * * * Type reply, press Enter. * * Reply . . . _____________________________________________________________* ********************************************************************************
Because this is a character field, you should enclose the value in apostrophes (e.g., 'DALLAS').
After you enter a value for INLOC, the system will prompt you to enter a value for INDEPT (e.g., 'IT'), after which you will see the results of the query (e.g., all employees working in the IT department at the Dallas location).
You can also specify the values for the parameters directly on the StrQMQry command, in which case the system does not prompt you for the values. The following snapshot of the StrQMQry command demonstrates how to enter the parametric values in the Set variables (SetVar) parameter.
Start Query Management Query (STRQMQRY)
Type choices, press Enter.
Query management query . . . . . > EMPLOYEES2 Name
Library . . . . . . . . . . . > UGGUTHRIE Name, *LIBL, *CURLIB
Output . . . . . . . . . . . . . * *, *PRINT, *OUTFILE
Query management report form . . *SYSDFT Name, *SYSDFT, *QMQRY
Library . . . . . . . . . . . Name, *LIBL, *CURLIB
Additional Parameters
Relational database . . . . . . *NONE
Connection Method . . . . . . . *DUW *DUW, *RUW
User . . . . . . . . . . . . . . *CURRENT Name, *CURRENT
Password . . . . . . . . . . . . Character value, *NONE
Naming convention . . . . . . . *SYS *SYS, *SQL, *SAA
Allow information from QRYDFN . *NO *NO, *YES, *ONLY
Set variables:
Variable name . . . . . . . . > INLOC
Variable value . . . . . . . . > '''DALLAS'''
Variable name . . . . . . . . > INDEPT
Variable value . . . . . . . . > '''IT'''
This technique gives your CL programs the ability to execute SQL statements and at the same time take advantage of the use of parameters. Remember, your CL program must enclose character parameter values in apostrophes as the following sample program demonstrates.
/* ==================================================================== */
/* = Sample CL program using StrQMQry command = */
/* ==================================================================== */
Pgm
( &Location +
&Department +
)
Dcl &Location *Char ( 10 )
Dcl &Department *Char ( 10 )
Dcl &InLoc *Char ( 12 )
Dcl &InDept *Char ( 12 )
ChgVar &InLoc ( '''' *Cat &Location *Cat '''' )
ChgVar &InDept ( '''' *Cat &Department *Cat '''' )
StrQMQry QMQry( YourLib/Employees2) +
SetVar( ( INLOC &INLOC ) ( INDEPT &INDEPT) )
EndPgm