primary files

5 pts.
Tags:
AS/400
RPG
Can anyone explain the concept of primary files in RPG , as i am a fresher in AS/400, i just want to know why do we use the Primary , secondary files concept ?

Answer Wiki

Thanks. We'll let you know when a new response is added.

Primary and Secondary files go back to the early days of RPG.

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.

With SQL SELECT, this can be done with a WHERE clause to specify key fields that should be equal. In RPG Cycle processing, it can be done with “matching records” (among other ways). Here, we tell RPG that PKEY and SKEY are the fields in the two files that are to match:<pre>
FPRIF IPE E DISK rename(PRIF:PRIR)
FSECF ISE E DISK rename(SECF:SECR)
FQPRINT O F 132 PRINTER
IPRIR 01
I PKEY M1
ISECR 02
I SKEY M1
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 the output now becomes:<pre>
Pri 00001 P1st 00000
Sec 00001 P1st 00001 S1st1
Sec 00001 P1st 00001 S1st2
Pri 00002 P2nd 00001 S1st2
Sec 00002 P2nd 00002 S2nd1
Sec 00002 P2nd 00002 S2nd2</pre>
With matching records, RPG processes primary records and matching secondary records in an interleaving way. The next primary record isn’t processed until the secondary records that match the first primary record are processed.

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.

Tom

Discuss This Question: 2  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
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
  • Vids
    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.
    295 pointsBadges:
    report
  • TomLiotta
    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
    125,585 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

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

REGISTER or login:

Forgot Password?
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

Thanks! We'll email you when relevant content is added and updated.

Following