The Create New Records (WRITE) command writes a new record to a file.
This command never runs in the traditional sense and will return an error if an attempt is made to run the command. The CLF precompilers, when they encounter this command in a CL source program, replace the command and generate CL instructions directly into the compiled CL program.
Restrictions:
- This command can only be used in a CL program that is created using a CLF precompiler command. The CLF precompiler commands are CRTBNDCLF, CRTCLFMOD, and CRTCLFPGM.
File identifier (FILEID)
Specifies the file identifier that was used on a previous Open File for Processing (OPEN) or Open File using CLF (OPNFCLF) command in the application.
This parameter is used by CLF commands to identify the file to be processed by the command.
If the file is declared using the DCLFCLF command this parameter is optional if the RCDFMT parameter specifies a record format name that is associated with one and only one file ID.
- simple-name
- Specify a name that matches the FILEID parameter value of a previous OPEN or OPNFCLF command.
Error found (ERR)
Specifies a CL logical variable to receive general error status. If the variable value is equal to '1' (true) then an error was found when performing the command. If the variable value is equal to '0' (false) then no error was found when performing the command.
This CL variable can be declared by the Declare Indicators using CLF (DCLINDCLF) command, the Generate Indicators using CLF (GENINDCLF) command, or by you using the Declare CL Variable (DCL) command. The two CLF commands would have specified CLFIND(*YES). When using the CLF commands the name of the CL variable is &ERR.
If no CL variable is specified and an error condition is encountered the application program will be sent an escape message.
- logical-value
- Specify the name of a CL variable declared as TYPE(*LGL).
Program opening file (OPNPGM)
The same FILEID value can be in use across multiple programs within a job. Each instance of a file open is uniquely identifed by the OPNPGM parameter. This parameter specifes the application program that initially opened the file using the Declare File using CLF (OPEN) or the Open File using CLF (OPNFCLF) command.
All CLF commands that reference the same FILEID and OPNPGM combination share the same view of the file. If one program causes the position within the file to change all programs see that change. If one program closes the file then the file is closed to all programs.
The combination of FILEID and OPNPGM must be unique within the activation group the application program is running in.
If the program name is *CURPGM then the library qualifier for this parameter is ignored. The library of the current program will be used.
Single values: Program opening file
- *CURPGM
- The current program opened the file associated with FILEID. It is this file open instance that is to be used.
Qualifier 1: Program opening file
- name
- Specify the name of the program that initially opened the file identified by FILEID. It is this file open instance that is to be used.
Qualifier 2: Library
- *CURLIB
- The current library for the thread is used. If there is no current library for the thread then library QGPL is used.
- name
- Specify the name of the library where the program that initially opened the file is located.
Examples for WRITE
Example 1: Write a Record to a Database File
ChgVar Var(&EmpNbr) Value(0001)
ChgVar Var(&EmpSts) Value('F')
ChgVar Var(&EmpFName) Value('Rebecca')
ChgVar Var(&EmpDpt) Value(' ')
ChgVar Var(&EmpDptM) Value('A1')
ChgVar Var(&EmpExt) Value('9044')
Write VC2EMP
Example 2: Writing to an Externally Described Print File
Pgm
/******************************************************/
/* This program creates a printed report listing */
/* employees in key sequence using the VC2EMPDPT */
/* access path (Dept/First name) */
/******************************************************/
/******************************************************/
/* Declare two working variables: */
/* &CURLIN - the current line number printed */
/* &PAGOVR - the overflow line number for a page */
/******************************************************/
Dcl Var(&CurLin) Type(*Dec) Len(3 0)
Dcl Var(&PagOvr) Type(*Dec) Len(3 0)
/******************************************************/
/* Declare our files and indicators */
/* VC2EMPDPT - Employees by department */
/* VC2POSPRT - External printer file */
/* CLF Indicators - &EOF used in program */
/******************************************************/
File FileID(VC2EMPDPT)
File FileID(VC2POSPRT)
Inds CLFInd(*Yes)
/******************************************************/
/* Open the files */
/******************************************************/
Open FileID(VC2EMPDPT) AccMth(*KEY)
Open VC2POSPRT Usage(*Output)
/******************************************************/
/* Retrieve the page overflow line number */
/******************************************************/
RtvFInfCLF FileID(VC2POSPRT) PrtFOvrFlw(&PagOvr)
/******************************************************/
/* Write titles for report on first page */
/******************************************************/
Write RcdFmt(HEADING)
/******************************************************/
/* Read the first record from VC2EMPDPT */
/******************************************************/
Read VC2EMPDPT EOF(&EOF)
DoWhile Cond(*Not &EOF)
/*******************************************/
/* While we have not reached End of File */
/* on VC2EMPDPT: */
/* 1 Retrieve the current print line number*/
/* 2 If the current print line number is */
/* greater than or equal to the overflow */
/* line number then start a new page and */
/* re-write titles on the page */
/* 3 Write the detail employee print line */
/* 4 Read the next employee record */
/* 5 Continue until End of File */
/*******************************************/
RtvFInfCLF VC2POSPRT CurPrtLine(&CurLin)
If Cond(&CurLin *GE &PagOvr) Then( +
Write RcdFmt(HEADING))
Write RcdFmt(DETAILS)
Read VC2EMPDPT EOF(&EOF)
EndDo
/******************************************************/
/* At End of File close the files and return */
/******************************************************/
Close OPNID(VC2EMPDPT)
Close VC2POSPRT
EndPgm
The example program opens the VC2EMPDPT logical file and creates a report using the external printer file VC2POSPRT. The report lists all employees in the CLF provided sample databases. The VC2EMPDPT logical file is keyed by employee first name within department.
The source for the example can be found in member RPG_POSPRT of source file VC2CLSRC in library VC2CLF. The source for logical file VC2EMPDPT can be found in member VC2EMPDPT of source file QDDSSRC in library VC2CLF. The source for printer file VC2POSPRT can be found in member VC2POSPRT of source file QDDSSRC in library VC2CLF.
The logical file VC2EMPDPT and printer file VC2POSPRT should currently exist in library VC2CLF. To compile the RPG_POSPRT program into QTEMP you can use the commands
AddLibLE Lib(VC2CLF)
CrtBndCLF Pgm(QTEMP/RPG_POSPRT) SrcFile(VC2CLSRC)
Example 3: Writing a Database Record by RRN
/******************************************************/
/* This program randomly reads the 12th record of the */
/* VC2EMP physical file by relative record number. */
/* The employee name found (Ruth) is displayed. The */
/* record is then deleted and an attempt to read the */
/* same record made. The record should not be found */
/* as it has been deleted and a message is sent based */
/* on whether or not the record is successfully read. */
/* The program then writes a new 12th record to add */
/* a new employee. The program then re-reads the 12th */
/* record and displays the employee name found (Chad) */
/******************************************************/
Pgm
/******************************************************/
/* Declare the file and CLF indicators */
/******************************************************/
File FileID(VC2EMP)
Inds CLFInd(*Yes)
/******************************************************/
/* Open the file for RRN access and update */
/******************************************************/
Open VC2EMP Usage(*Both)
/******************************************************/
/* Read the 12th record and display the name found */
/******************************************************/
Chain RRN(12) FileID(VC2EMP)
SndPgmMsg Msg(&EmpFName *BCat 'found')
/******************************************************/
/* Delete the record read and try to re-read it. */
/* A record-not-found condition should be returned. */
/******************************************************/
Delete FileID(VC2EMP)
Chain RRN(12) FileID(VC2EMP) RcdNotFnd(&RNF)
If Cond(&RNF) Then( +
SndPgmMsg Msg('Record not found'))
Else Cmd(SndPgmMsg Msg( +
&EmpFName *BCat 'found after delete'))
/******************************************************/
/* Write a new record at the deleted position (RRN 12)*/
/******************************************************/
ChgVar Var(&EmpFName) Value('Chad')
ChgVar Var(&EmpSts) Value(F)
Write RRN(12) FileID(VC2EMP)
/******************************************************/
/* Read the 12th record and display the name found */
/******************************************************/
Chain RRN(12) FileID(VC2EMP)
SndPgmMsg Msg(&EmpFName *BCat 'found after write')
/******************************************************/
/* Close the file and return */
/******************************************************/
Close VC2EMP
EndPgm
The source for this example can be found in member RPG_RRNUSG of source file VC2CLSRC in library VC2CLF. The source for file VC2EMP can be found in member VC2EMP of source file QDDSSRC in library VC2CLF.
The VC2EMP file should currently exist in library VC2CLF. To compile RPG_RRNUSG into library QTEMP you can use the commands
AddLibLE Lib(VC2CLF)
CrtBNDCLF Pgm(QTEMP/RPG_RRNUSG) SrcFile(VC2CLSRC)
Prior to running RPG_RRNUSG you should reload the VC2EMP database file to ensure that the expected results are seen. To reload the file you can refer to the source member DEV_LOAD in source file VC2CLF/VC2CLSRC or Appendix D of the CLF Programmer's Guide.
After reloading the VC2EMP file you can call RPG_RRNUSG with the command
Call Pgm(QTEMP/RPG_RRNUSG)
You should see the following messages displayed
Ruth found
Record not found
Chad found after write