Cozzi's Run SQL using QM Query (RUNQMQRY)

Where allowed to run: All environments (*ALL)
Threadsafe: No
Parameters
Examples
Error messages

Cozzi's Run SQL using QM Query (RUNQMQRY) command runs most SQL statements using the IBM i QM Query engine, including the SELECT statement. Have you considered using the more powerful SQL iQuery? Learn more at: http://www.coztools.com/iQuery

RUNQMQRY creates a temporary *QMQRY object and then uses the STRQMQRY command to run the SQL statement. Most SQL statements may be specified, including but not limited to:

Restrictions:

Top

Parameters

Keyword Description Choices Notes
SQL SQL statement Character value Required, Positional 1
OUTPUT Output *, *PRINT, *FILE, *OUTFILE Optional, Positional 2
NAMING Naming *SYS, *SQL Optional, Positional 3
RDB Remote Relational database Character value, *LOCAL, *NONE Optional, Positional 4
USER Remote Database User ID Character value, *USER, *USRPRF, *CURRENT Optional, Positional 5
PWD Remote Database Password Character value, *NONE Optional, Positional 6
OUTFILE File to receive output Qualified object name Optional, Positional 7
Qualifier 1: File to receive output Name
Qualifier 2: Library Name, *CURLIB, *LIBL
OUTMBR Output member and option Element list Optional, Positional 8
Element 1: Member Name, *FIRST
Element 2: Replace or add records *REPLACE, *ADD
Top

SQL statement (SQL)

Consider using SQL iQuery - LICPGM(2COZ-IQ3) The better choice for Command Line SQL and Reports. http://www.SQLiQuery.com

Specify any SQL statement that you wish to run. The statement is copied to a source file member in QTEMP and then converted into a *QMQRY object for processing. The maximum SQL statment length supported is 5000 characters.

This is a required parameter.

character-value
Specify the SQL statement to be run.
Top

Output (OUTPUT)

Consider using SQL iQuery - LICPGM(2COZ-IQ3) The better choice for Command Line SQL and Reports. http://www.SQLiQuery.com

Specifies for SELECT statements only, the output device for the result set. By default output is sent to the display for interactive jobs, and to a SPOOL file (print) for a batch job.

*
The resultset of a SELECT statement is displayed on the workstation.
*PRINT
The resultset of a SELECT statement is printed to the default print device.
*FILE or *OUTFILE
The resultset of a SELECT statement is routed to the database file and member specified on the OUTFILE and OUTMBR parameters.
Top

Naming (NAMING)

Consider using SQL iQuery - LICPGM(2COZ-IQ3) The better choice for Command Line SQL and Reports. http://www.SQLiQuery.com

Specifies SQL naming style being used in your SQL statement.

*SYS
Using IBM i (AS/400) qualified names in the format: library/file
*SQL
Using the SQL standard qualified names: library.file
Top

Remote Relational database (RDB)

Consider using SQL iQuery - LICPGM(2COZ-IQ3) The better choice for Command Line SQL and Reports. http://www.SQLiQuery.com

Specifies the remote system where the tables/files being queried exist. This entry may be *LOCAL or one of the values in the system's remote database directory. Use the WRKRDBDIRE command to view the available remote database choices.

*LOCAL or *NONE
No remote database is used, the database on the local system is accessed. files.
character-value
Specify the remote database entry to connect to. When this option is specified, then the USER and PASSWORD parameters are also required.
Top

Remote Database User ID (USER)

Consider using SQL iQuery - LICPGM(2COZ-IQ3) The better choice for Command Line SQL and Reports. http://www.SQLiQuery.com

When the RDB parameter contains a remote database name, then this parameter must contain the user profile that is used to connect to that remote system. When the RDB parameter is RDB(*LOCAL or *NONE) than this parameter is ignored.

*CURRENT or *USER or *USRPRF
The current user for the job running the RUNQMQRY command is used as the remote user profile.
character-value
Specify the user profile to be used to CONNECT TO the remote database system.
Top

Remote Database Password (PWD)

Consider using SQL iQuery - LICPGM(2COZ-IQ3) The better choice for Command Line SQL and Reports. http://www.SQLiQuery.com

Specifies password for the user profile being used to connect to the remote database system. This parameter is ignored when the RDB parameter is *LOCAL or *NONE.

*NONE
No user profile is sepcified. This value is valid only when RDB(*LOCAL) is specified and is invalid when RDB contains a remote database system name.
character-value
Specify the password for the user profile specified on the USER parameter.
Top

File to receive output (OUTFILE)

Consider using SQL iQuery - LICPGM(2COZ-IQ3) The better choice for Command Line SQL and Reports. http://www.SQLiQuery.com

Specifies the name of a database physical file where the output from an SQL SELECT statement is written. For non-SELECT statements, this parameter is ignored.

Qualifier 1: File to receive output

name
Specify the name of a DB2 for i databse file that exists or will be created that receives the results of the SQL SELECT statement. If the file does not exist, it is automatically created. If the file exists, it must have the same format as produced by the SQL SELECT statement or the command will fail.

Qualifier 2: Library

*CURLIB
The database output file is located in the job's CURRENT LIBRARY. By default, if no CURRENT LIBRARY is specified for the job, then QGPL is used.
*LIBL
All libraries in the job's library list are searched until the database file is found. If it is not found, the file/table is created in the *CURLIB (current library) for the job or QGPL if no *CURLIB attribute is specified.
name
Specify the name of library where the output database file exists or shall be created.
Top

Output member and option (OUTMBR)

Consider using SQL iQuery - LICPGM(2COZ-IQ3) The better choice for Command Line SQL and Reports. http://www.SQLiQuery.com

Specifies name of the database member that recieves the result set from the SQL SELECT statement. The ADD/REPLACE option is also specified on this parameter.

Element 1: Member

*FIRST
The first member in the existing output file is used as the target for output from the SQL SELECT statement. If the file does not exist, the member created is the same as the database file name.
name
Specify the name of member in the output database file. The user-specified member name is used.

Element 2: Replace or add records

*REPLACE
Existing records in the database file member are REPLACED with the data from the SQL SELECT statement.
*ADD
The records in the SQL SELECT result set are added to the database file memeber--existing records are preserved.
Top

Examples for RUNQMQRY

Consider using SQL iQuery - LICPGM(2COZ-IQ3) The better choice for Command Line SQL and Reports. http://www.SQLiQuery.com

Example 1: Simple Command Example

RUNQMQRY  SQL('SELECT * FROM QIWS/QCUSTDT') OUTPUT(*PRINT)

Learn more at: http://www.SQLiQuery.com

This example prints a listing of the records in the sample IBM-supplied database table QCUSTDT in the QIWS library.

Example 2: More Complex Command Example

RUNQMQRY  SQL('SELECT * FROM QIWS/QCUSTDT') OUTPUT(*FILE)
           OUTFILE(MYLIB/CUSTOMERS) OUTMBR(*FIRST *REPLACE)

This command copies the records in the result set to the file named CUSTOMERS in library MYLIB. The first member in the file receives the data, and if any records exist in the file, they are replaced with the data from this SQL statement.

Example 3: More Complex Command Example

RUNQMQRY  SQL('UPDATE CUSTOMERS SET LASTORD = CURRENT DATE
               WHERE  ORDSTATUS = 1')

This command update the CUSTOMERS file by setting the LASTORD (last order date) field to today's date. It does this for all recods that have an ORDSTATUS of 1. NOTE: The RUNQMQRY command does not provide support to control the COMMIT parameter. This is a limitation in the STRQMQRY command itself. As an alternative, consider using SQL iQuery's RUNIQRY command (which does offer the COMMIT parameter) or the IBM-supplied RUNSQL command.

Top

Error messages for RUNQMQRY

*ESCAPE Messages

CPF9801
Object &2 in library &3 not found.
CPF9802
Not authorized to object &2 in &3.
CPF9803
Cannot allocate object &2 in library &3.
CPF9807
One or more libraries in library list deleted.
CPF9808
Cannot allocate one or more libraries on library list.
CPF9810
Library &1 not found.
CPF9811
Program &1 in library &2 not found.
CPF9812
File &1 in library &2 not found.
CPF9820
Not authorized to use library &1.
CPF9830
Cannot assign library &1.
CPF9899
Error occurred during processing of command.
Top