My favorite API has to be QCMDEXC. I think it was the first API I used when I when I was making the transition from RPGII to RPGIII many, many years ago. I use it so often I think half the programs I write have it in them. QCMDEXC allows me to execute any CL command that does not return any values.

For several releases there has been a SQL procedure QCMDEXC, which does the same as the API. Then in IBM i 7.1 TR7 there was an update to the SQL procedure. You will find that I have used it in several examples in prior posts, but I have not given it its own post until now. I have to confess that I am so accustomed to using the API program version of QCMDEXC, I keep forgetting to use the SQL procedure.

There are alternatives, I have written about them in the past, SYSTEM() and QCAPCMD . But I always find myself returning to QCMDEXC just due to its ease of use.

If I use the SQL procedure I do not have to give a procedure definition, as I do with the API, and it only needs one parameter, the command string to execute. I no longer have to determine how long the command string is and pass it at a second parameter as I do using the API program:

01 dcl-pr QCMDEXC extpgm ; 02 *n char(500) options(*varsize) const ; 03 *n packed(15:5) const ; 04 end-pr ; 05 dcl-s Command char(500) ; 06 dcl-s Length packed(15:5) ; 07 dcl-s File char(10) ; 21 Command = 'CLRPFM QTEMP/' + File ; 22 QCMDEXC(Command:%len(%trimr(Command))) ;

For a detailed explanation of how to define the QCMDEXC API as an external program check out the post about calling programs in totally free RPG.

The equivalent using the QCMDEXC SQL procedure would just be:

01 dcl-s Command char(500) ; 02 dcl-s File char(10) ; 21 Command = 'CLRPFM QTEMP/' + File ; 22 exec sql CALL QSYS2.QCMDEXC(:Command) ;

I can even give the command string I wish to execute in the SQL procedures parameter:

exec sql CALL QSYS2.QCMDEXC('CLRPFM QTEMP/WORKFILE') ;

Being a lazy programmer I love that it is so simple. Now I just have to remember to use it every time!

For those of you using a version older of IBM i than 7.1 TR7 you can still use the SQL procedure, but you have to give the length of the command string in a second parameter:

Command = 'CLRPFM QTEMP/' + File ; Length = %len(%trimr(Command)) ; exec sql CALL QSYS2.QCMDEXC(:Command,:Length) ; exec sql CALL QSYS2.QCMDEXC('RCLRSC',6) ;

You can learn more about the QCMDEXC SQL procedure from the IBM website here.

This article was written for IBM i 7.3, and should work for earlier releases with some minor modifications.