You Can View User Feedback To This Tip
In a previous tip, Who said you can't write to file from CL?
Clive Griffiths
published his WRTREC command/CL combination which allows one to write a
record to a physical file from a CL program.
I have been using this code for a few years and it is very useful. The implementation seemed very complex for a simple idea. I came
up with an alternative that runs five times faster according to my primitive
testing. My implementation uses the QSH "echo" command, redirecting
output to the physical file in "append" mode.
/*********************************************************/
/* PGM - WRTRECCPP */
/* AUTHOR - CLIVE GRIFFITHS */
/* MODIFIED BY - STEVE RIEDMUELLER 06/13/05 */
/* DESC - WRITE A RECORD TO SEPCIFIED FILE */
/* PARMS - &RECORD 100 CHARACTER - DATA TO BE WRITTEN TO FILE */
/* - &TOLIB 10 CHARACTER - LIBRARY NAME OF FILE TO BE WRITTEN*/
/* - &TOFILE 10 CHARACTER - NAME OF FILE TO BE WRITTEN TO */
/*********************************************************************/
PGM (&RECORD &TOFILE &TOLIB)
DCL &RECORD *CHAR 100
DCL &TOFILE *CHAR 10
DCL &TOLIB *CHAR 10
DCL &TARGET *CHAR 100
DCL &CMD *CHAR 100
CHGVAR VAR(&TARGET) VALUE('/qsys.lib/' *TCAT &TOLIB +
*TCAT '.lib/' *TCAT &TOFILE *TCAT +
'.file/' *TCAT &TOFILE *TCAT '.mbr')
CHGVAR VAR(&CMD) VALUE('echo "' *TCAT &RECORD *TCAT +
'" >> ' *CAT &TARGET)
QSH CMD(&CMD)
ENDPGM
USER FEEDBACK TO THIS TIP
- I would like to add a correction to this tip.
1. Qshell should be installed (ss1 33)
2. Max rec length 512
Code: CMD PROMPT('Add a record to a file')
PARM KWD(DATA) TYPE(*CHAR) LEN(512) MIN(1) +
CHOICE('Enter data to be written') +
PROMPT(DATA)
PARM KWD(TOLIB) TYPE(*NAME) LEN(10) MIN(1) +
CHOICE('Library name') PROMPT(TOLIB)
PARM KWD(TOFILE) TYPE(*NAME) LEN(10) MIN(1) +
CHOICE('Target file name') PROMPT(TOFILE)
PARM KWD(TOMEMBER) TYPE(*NAME) LEN(10) MIN(1) +
CHOICE('Member name') PROMPT(TOMEMBER)
-------------------------------------------------------------------------------------------------------
PGM PARM(&RECORD &LIB &FILE &MEMBER)
DCL VAR(&RECORD) TYPE(*CHAR) LEN(512)
DCL VAR(&LIB) TYPE(*CHAR) LEN(10)
DCL VAR(&FILE) TYPE(*CHAR) LEN(10)
DCL VAR(&MEMBER) TYPE(*CHAR) LEN(10)
DCL VAR(&DATA) TYPE(*CHAR) LEN(100) VALUE(' ')
ADDENVVAR ENVVAR(TM) VALUE(&RECORD) REPLACE(*YES)
CHGVAR VAR(&DATA) VALUE('echo $TM >>' *BCAT +
'/qsys.lib/' *TCAT &LIB *TCAT '.lib/' +
*TCAT &FILE *TCAT '.file/' *TCAT &MEMBER +
*TCAT '.mbr/')
QSH CMD(&DATA)
RMVENVVAR ENVVAR(TM)
MONMSG MSGID(CPFA981)
ENDPGM
Avi Hecht
- CL has been able to do a few forms of writing to files for many years. CL can call the Sort (QLGSORT) API and pass records into the API through the input buffer -- the API can send those to output files which can be copied/appended to other files. CL can call QM queries to effect SQL INSERTs to files.
But in Version 5 of OS/400, CL got even closer through the QShell DB2 utility (see example #1). And with a little research, you can see that the DB2 utility is a symbolic link to program QZDFMDB2 (see example #2).
Still not perfect, but pretty workable.
#1
QSH CMD('db2 "INSERT INTO QIWS.QCUSTCDT (CUSNUM, LSTNAM, INIT) VALUES(876543, ''Liotta'', ''T'')"')
#2
CALL QZDFMDB2 ('INSERT INTO TOML.QCUSTCDT (CUSNUM, LSTNAM, INIT) VALUES(876543, ''Liotta'', ''T'') ')
Tom Liotta
==================================
MORE INFORMATION ON THIS TOPIC
==================================
The Best Web Links: Tips, tutorials and more.
Visit the ITKnowledge Exchange and get answers to your systems management questions fast.
Read this Search400.com Featured Topic: Take control of your iSeries.
Ask the Experts yourself: Our systems management gurus are waiting to answer your technical questions.