Basically, the RPG program automaticallty reads in the file one record at a time and your code is applied for each record.

Certain indicators (LR, L1, L2, etc.) allow you to condition code for Level breaks (subtotals) and last record (totals)

This is also known as CYCLE programming.

================================================================

Primary (and secondary) files are from the earliest RPG and RPG II languages, from before the CHAIN op-code was first released. A primary file is to RPG what the FROM table is to SQL. A primary file is the first file that RPG automatically reads. Much of the idea behind RPG primary and secondary files can be described after running this SQL from a source member with RUNSQLSTM:<pre>
CREATE TABLE MYLIB/PRIF (
PKEY NUMERIC ( 5 ) NOT NULL WITH DEFAULT,
PVAL CHAR ( 5 ) NOT NULL WITH DEFAULT
)
;
INSERT INTO MYLIB/PRIF VALUES(1, ‘P1st’) ;
INSERT INTO MYLIB/PRIF VALUES(2, ‘P2nd’) ;

CREATE TABLE MYLIB/SECF (
SKEY NUMERIC ( 5 ) NOT NULL WITH DEFAULT,
SVAL CHAR ( 5 ) NOT NULL WITH DEFAULT
)
;
INSERT INTO MYLIB/SECF VALUES(1, ‘S1st1′) ;
INSERT INTO MYLIB/SECF VALUES(1, ‘S1st2′) ;
INSERT INTO MYLIB/SECF VALUES(2, ‘S2nd1′) ;
INSERT INTO MYLIB/SECF VALUES(2, ‘S2nd2′) ;</pre>
That creates two tables in MYLIB — a “primary” named PRIF, and a “secondary” named SECF. They have columns we can call ‘keys’ named PKEY and SKEY. They can be used to process the tables together in a SELECT statement run with interactive STRSQL:<pre>
SELECT * FROM MYLIB/PRIF, MYLIB/SECF WHERE PRIF.PKEY = SECF.SKEY</pre>
The result:<pre>
PKEY PVAL SKEY SVAL
————– —– ————– —–
000001 1 P1st 1 S1st1
000002 1 P1st 1 S1st2
000003 2 P2nd 2 S2nd1
000004 2 P2nd 2 S2nd2</pre>
That can be the same result that RPG obtains by having PRIF as primary and SECF as secondary with PKEY and SKEY as the match fields for ‘matching records’ A primary file can be processed by itself, but the concept of ‘primary’ can’t be fully described until ‘secondary’ is included.

At the simplest level, investigation might start with straight primary and secondary processing:<pre>
FPRIF IPE E DISK rename(PRIF:PRIR)
FSECF ISE E DISK rename(SECF:SECR)
FQPRINT O F 132 PRINTER
OQPRINT D
O PKEY +2
O PVAL +2
O SKEY +2
O SVAL +2</pre>
That’s not a good example of RPG Cycle programming, but it is a good example for study. Like the SQL SELECT statement, you can see that there is no OPEN for either file, no READ, no CLOSE and no WRITE for the printed output. Nor are there any calculations based on any of the values. The program simply processes the two files and prints an output line every “detail” cycle. The output is useless for anything but study, but it can be compared with the Cycle folwchart in every RPG Reference manual:<pre>
00000 00000
00001 P1st 00000
00002 P2nd 00000
00002 P2nd 00001 S1st1
00002 P2nd 00001 S1st2
00002 P2nd 00002 S2nd1
00002 P2nd 00002 S2nd2</pre>
That’s all there is. No headings because I didn’t include any in the program. Some values are zero or blank because records aren’t available during some detail cycles. The PKEY and PVAL values are repeated because they’re still in memory rather than because the record was read again.

We can clean it up a little by linking the output to one record or the other. This can be done by assigning “record identifying indicators” to the two files:<pre>
FPRIF IPE E DISK rename(PRIF:PRIR)
FSECF ISE E DISK rename(SECF:SECR)
FQPRINT O F 132 PRINTER
IPRIR 01
ISECR 02
OQPRINT D 01
O OR 02
O PKEY +2
O PVAL +2
O SKEY +2
O SVAL +2</pre>
The new output now occurs only on a detail cycle that occurs after a record from one of the files is brought into program memory:<pre>
00001 P1st 00000
00002 P2nd 00000
00002 P2nd 00001 S1st1
00002 P2nd 00001 S1st2
00002 P2nd 00002 S2nd1
00002 P2nd 00002 S2nd2</pre>
That’s a little cleaner, but not good enough. Let’s add an identifying code to output lines to see when each line is printed. This helps to see if a line comes during a primary or secondary cycle. We simply condition one constant or another on the print line by one of the two record identifying indicators:<pre>
FPRIF IPE E DISK rename(PRIF:PRIR)
FSECF ISE E DISK rename(SECF:SECR)
FQPRINT O F 132 PRINTER
IPRIR 01
ISECR 02
OQPRINT D 01
O OR 02
O 01 ‘Pri’
O 02 ‘Sec’
O PKEY +2
O PVAL +2
O SKEY +2
O SVAL +2</pre>
And now the print lines are tagged:<pre>
Pri 00001 P1st 00000
Pri 00002 P2nd 00000
Sec 00002 P2nd 00001 S1st1
Sec 00002 P2nd 00001 S1st2
Sec 00002 P2nd 00002 S2nd1
Sec 00002 P2nd 00002 S2nd2</pre>
This helps show that primary records are processed before secondaries, just like the order of files in a SELECT FROM clause determines the order of processing.

But it’s still not particularly useful. Each primary record is processed before any secondary record is processed at all. Then all secondary records are processed while just the last primary record is still in memory. We need a way to bring related primary and secondary records into memory at the same time.

It’s still not extremely useful, and it’s clearly more complex than a SELECT statement with a WHERE clause. But the SELECT needs some kind of report writer; it doesn’t actually do anything by itself. And I didn’t have any C-specs at all, and those can be tricky to put in the middle of a SELECT. Also, the RPG can easily be modified to handle records that match and don’t match in the same program. And there are level breaks available that weren’t demonstrated. And the output can have heading lines and total lines. And there can be output files instead of or in addition to the printer output.

All of that wraps around the basic concept of primary and secondary files and the RPG Cycle. Old technology, yes. Interesting to me, though, that the same concept is so acceptable without a second thought in SQL.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States.
Privacy

Processing your response...

Discuss This Question: 2 &nbspReplies

There was an error processing your information. Please try again later.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States.
Privacy

An example of Primary file could be Program described Printer File for hich we use O spec to define printer file and its fields.
For more details you can refer IBM Redbooks like - RPG Programmers Guide and RPG Reference. There can be only one Primary File and rest will be secondary files or externally described files in RPG.
As said in previous answer we need to use indicators for Primary Described File.

A Program described Printer File for hich we use O spec would most definitely not be a Primary file. No printer file is ever likely to be a primary file, and primary files won't be described with O-specs.
Tom

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States.
Privacy

Processing your reply...

Ask a Question

Free Guide: Managing storage for virtual environments

Complete a brief survey to get a complimentary 70-page whitepaper featuring the best methods and solutions for your virtual environment, as well as hypervisor-specific management advice from TechTarget experts. Don’t miss out on this exclusive content!

Share this item with your network:

To follow this tag...

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States.
Privacy