Beginners meeting SELECT for the first time often assume one verb with one meaning—like SQL SELECT lists columns from tables. In Easytrieve Report Generator the keyword SELECT appears in four distinct contexts: a one-word flag during SORT prescreening, the same one-word flag in REPORT-INPUT procedures, a full SQL clause for automatic relational input, and file-based SQL on SQL FILE definitions. Confusing them produces subtle bugs—your sort file empties because the BEFORE procedure never executed SELECT, or your report skips half the PRINT rows because REPORT-INPUT filtered without matching business rules. This page separates each SELECT flavor, explains the switch semantics Broadcom documents (SELECT sets inclusion for later processing, duplicate SELECT still yields one row, SELECT followed by STOP cancels inclusion), and walks through sort screening, report filtering, and JOB INPUT SQL patterns with examples a new maintainer can trace line by line.
| Context | Format | When required |
|---|---|---|
| Sort selection | SELECT (alone) | SORT ... BEFORE proc—per record you want sorted |
| Report selection | SELECT (alone) | REPORT-INPUT PROC—per PRINT input you want in report |
| Non-file SQL | SELECT ... FROM ... INTO :host-vars | JOB INPUT SQL—first statement in JOB |
| File-based SQL | On SQL FILE / cursor patterns | SQL FILE automatic or START procedure SELECT |
SORT normally copies every input record to the sorted output file in key order. When you need only a subset—or want to modify records before they enter the sort—you code SORT ... BEFORE proc-name. Easytrieve supplies input records to that procedure one at a time. For each record you want in the output file, you must execute SELECT. Omit SELECT and the record is dropped from sort output even though the procedure ran. The sort utility still receives only selected records when the sort phase executes.
1234567891011121314FILE PERSNL FB(150 1800) %PERSNL FILE SORTWRK FB(150 1800) VIRTUAL COPY PERSNL SORT PERSNL TO SORTWRK USING + (REGION, BRANCH, DEPT, NAME-LAST, NAME-FIRST) + NAME MYSORT BEFORE SCREENER SCREENER. PROC IF MARITAL-STAT = 'S' AND SEX = 1 SELECT END-IF END-PROC
SCREENER keeps only single female employees in the sorted work file. Major-to-minor key order in USING controls break hierarchy: REGION major, BRANCH minor, DEPT more minor, then name fields. D suffix on a key field requests descending order for that key only. Variable-length fields and certain index types cannot be sort keys—verify DEFINE attributes before coding USING.
SELECT only sets a switch; it does not immediately write the record. If logic SELECTs the same record twice in one procedure invocation, the record still appears once in sorted output. If you SELECT and later issue STOP in the same procedure pass, the record is not selected—STOP cancels the pending inclusion. This interacts with STOP used to cap sort volume: a procedure that STOPs when RECORD-COUNT exceeds fifty ends prescreening and runs the sort; any record mid-procedure where STOP follows SELECT loses that selection.
Broadcom limits field references in sort procedures to fields of the input file (plus working storage you use for tests). You cannot reference sort output file fields that do not exist yet. GET and PUT are among I/O statements invalid in SORT-invoked procedures—use SELECT, MOVE, IF, and DISPLAY instead. Keep BEFORE procedures fast; prescreening millions of rows with complex IF trees may push teams toward DFSORT INCLUDE/OMIT instead—compare with your easytrieve-vs-dfsort guidance when performance tuning.
REPORT-INPUT is a special-name report procedure performed for each PRINT statement when coded. To include the current report input in further report processing, execute SELECT inside REPORT-INPUT. Unselected input bypasses continued processing for that PRINT cycle. If you omit REPORT-INPUT entirely, PRINT-driven records flow into the report under default rules without an explicit SELECT per record.
123456REPORT-INPUT. PROC IF ZIP NE HOLD-ZIP HOLD-ZIP = ZIP SELECT END-IF END-PROC
This pattern prints detail only when ZIP changes—classic control-break filtering before LINE formatting. HOLD-ZIP working storage remembers the prior value. Duplicate SELECT on the same logical input still produces one printed occurrence, matching sort selection switch behavior.
PRINT names a report and triggers input retrieval for that report. REPORT-INPUT SELECT decides whether the retrieved row participates in totals and LINE output for that pass. Teams sometimes duplicate logic in JOB body IF and REPORT-INPUT—consolidate on one layer to avoid skew where JOB prints diagnostics but report suppresses lines, or vice versa.
JOB INPUT SQL declares automatic SQL retrieval without naming a Library SQL FILE. Exactly one non-file SQL SELECT may appear per JOB activity, and it must be the first statement in the JOB body immediately following the JOB header. The SELECT lists columns, FROM tables, optional WHERE, GROUP BY, HAVING, UNION, ORDER BY, and mandatory INTO :host-variable list for fetched columns.
123456789JOB INPUT SQL NAME SQL-JOB SELECT EMPNAME, SALARY FROM EMP WHERE DEPT = 100 INTO :EMP-NAME :EMP-SAL IF SQLCODE NE 0 DISPLAY 'SQL FETCH ISSUE' END-IF PRINT DEPT100-RPT
Easytrieve generates DECLARE CURSOR, OPEN, FETCH loop, and CLOSE around your JOB logic. SQLCODE 100 signals end of data—FINISH procedure runs, reports wrap, JOB ends. Other SQLCODE values produce diagnostics and terminate execution. On SQL/DS, CONNECT may be generated using PARM USERID credentials. The cursor closes after the JOB activity completes.
When you define an SQL FILE in Library, automatic input or START procedure may issue SELECT to position the cursor. FILE DEFER delays open until first I/O—use DEFER when START procedure must run SELECT before default cursor open to avoid double-open overhead documented in OPEN statement guidance. File-based SELECT grammar parallels non-file SELECT but ties to the FILE name on JOB INPUT or explicit cursor management in procedures.
Sometimes SELECT is a tiny sticker you put on a paper saying yes, this one goes in the box. At sort time, only papers with stickers get sorted into the new pile. At report time, only stickered papers get printed in the story. That sticker is just the word SELECT on its own line. Other times SELECT is a big sentence asking the database for rows—that is a different game with the same word on the jersey. Learn which game you are playing before you code.
1. In a SORT BEFORE procedure, SELECT:
2. If you SELECT the same record twice in one pass:
3. SELECT then STOP in a sort procedure:
4. Non-file SQL SELECT must follow:
5. REPORT-INPUT SELECT controls: