Reports and data

Turn a file into a report, then ask SQL for more.

Query/400 saves a report definition and runs it with RUNQRY. Interactive SQL runs ad hoc statements and prompts you for the parts you cannot remember.

Create data to report on

Both tools read the same Db2 files. Create a small table and fill it with three rows from CL; RUNSQL is the quickest way to run one statement outside STRSQL.

CRTLIB APP TYPE(*TEST)
RUNSQL SQL('CREATE TABLE APP.SALES (REGION VARCHAR(10), ITEM VARCHAR(8), QTY INT)') COMMIT(*NONE)
RUNSQL SQL('INSERT INTO APP.SALES VALUES (''North'', ''Bolt'', 10), (''North'', ''Nut'', 5), (''South'', ''Bolt'', 7)') COMMIT(*NONE)

Run Query/400 directly

RUNQRY can build a report from a file on the spot, without a saved definition. OUTTYPE chooses the destination: *DISPLAY shows the report, *PRINTER writes a spooled file and *OUTFILE writes a database file.

RUNQRY QRYFILE((APP/SALES)) OUTTYPE(*DISPLAY)
RUNQRY QRYFILE((APP/SALES)) OUTTYPE(*PRINTER)
RUNQRY QRYFILE((APP/SALES)) OUTTYPE(*OUTFILE) OUTFILE(APP/TOTALS *FIRST *NEWFILE)
DSPFFD FILE(APP/TOTALS)
WRKSPLF

The display report ends with ******** End of report ********. In a 5250 session it opens the Display Report panel; F3 and F12 leave it. The printer report is QPQUPRFIL in your output queue.

Add RCDSLT(*YES) to see the Select Records panel before the report runs. The panel supports EQ, NE, LT, LE, GT, GE, IS and ISNOT tests. F12 cancels without producing output. A displayed report can return to the selection panel to try another selection for this run; the saved query itself is not changed.

RUNQRY QRYFILE((APP/SALES)) OUTTYPE(*PRINTER) RCDSLT(*YES)

Build a saved query with WRKQRY

WRKQRY creates a *QRYDFN object that RUNQRY can run later. Enter WRKQRY, type option 1, a query name such as SALESQ and library APP, then press Enter. On Define the Query, type 1 next to the tasks you need and press Enter after each one:

  1. Specify file selections: File SALES, Library APP, member *FIRST.
  2. Select and sequence fields: give REGION sequence 10 and QTY sequence 20.
  3. Select records: field QTY, test GT, value 5.
  4. Select sort fields: priority 1 on REGION, order A.
  5. Select report summary functions: function 1 (Total) on QTY.
  6. Define report breaks: break level 1 on REGION.
  7. Select output type and output form: 2=Printer, 1=Detail, or 3=File for a database member.

F5 on Define the Query runs a displayed report without saving. F3 opens Exit this Query, where SAVE=Y stores the definition; RUN=Y runs it immediately. The saved query keeps its own file selection, fields, selection tests, sort order, summaries, breaks and output type.

RUNQRY QRY(APP/SALESQ)
RUNQRY QRY(APP/SALESQ) OUTTYPE(*OUTFILE) OUTFORM(*SUMMARY) +
  OUTFILE(APP/TOTALS *FIRST *NEWFILE)

The summary run writes one record per break, with BREAKLVL, OVERFLOW and the break and summary fields. Use *RPLFILE instead of *NEWFILE to replace an existing output file, or *NEWMBR, *RPLMBR and *ADDMBR to choose a member. PRTDFN(*YES) spools the query definition together with the report. RUNQRY also accepts printer overrides (PRTDEV, FORMSIZE, FORMTYPE, COPIES and LINESPACE) and AUT, the public authority of an output file it creates.

Interactive SQL in depth

Enter STRSQL NAMING(*SQL). Statement entry accepts free-form SQL one statement at a time. F9 recalls an earlier statement, F6 inserts a line, F10 copies it, F14 deletes it and F15 splits it at the cursor. F16, F17 and F18 browse library, table and column names and insert the selection at the cursor.

CREATE TABLE APP.PARTS (ITEM CHAR(8), QTY INT)
INSERT INTO APP.PARTS VALUES ('BOLT', 10), ('NUT', 5)
SELECT * FROM APP.PARTS WHERE QTY > 5

Type SELECT or INSERT and press F4 to open a statement prompt. F4 on the FROM field lists tables; F4 on the column fields opens Select and Sequence Columns. Enter runs the statement through the session's processing mode. F3 on the data display returns to statement entry.

Session services and output

F13 opens Interactive SQL Session Services. Option 1, Change Session Attributes, controls statement processing (*RUN, *VLD to validate without running, *SYN to parse only), SELECT output (1=Display, 2=Printer, 3=File), commitment control, naming convention, date and time formats, library list scope and list type. Option 2 prints the current session and option 4 saves it in a source file.

F3 or F12 opens Exit Interactive SQL. Option 1 saves the statement history and attributes with your profile for the next STRSQL; option 2 discards them; option 3 resumes; option 4 writes the statements to a source member and exits. Saved sessions are per profile and survive an IPL. Commitment-controlled changes prompt for an explicit commit or rollback before exit.

For file output, the session creates or replaces a physical file and writes the selected rows with null indicators. Create and replace file or member, and append, are available. Destination files that have keys, journals, triggers or constraints are refused with CPF9899.

What is not supported yet

Continue with the first SQL table, the Query/400 lesson and the interactive SQL lesson. The command pages for RUNQRY, WRKQRY and STRSQL list every parameter.