In an earlier tip I have illustrated how easy it is to execute a DB2 command from REXX. What if I want to execute the DB2 command from COBOL or PL/I instead? That is a quite different ball game. It is actually so difficult that I almost will advise you not to do it. If you cannot control your curiousity I will now present to you a piece of code which executes a -DIS GROUP from PL/I. Before the code slice will work you must issue a CAF CONNECT to the DB2 subsystem you want to execute the command against:
The result of the -DIS GROUP DB2 command is returned in return_area. You have to decode the contents yourself if you want to format it nicely. It is the call to dsnwli which carries out the DB2 command. The areas ifca, return_area and output_area has to be declared as shown. You may declare rtrn_buff bigger, but remember to assign the correct length of rtrn_buff to return_area.lngth. The value of output_area.lngth must be 4 greater than the length of the command text padded with one space. This is why I have used 4 + 11 as -DIS GROUP is ten characters long. The variables in ifca must be assigned the values I have specified in the code slice.
By the way it is possible to call dsnwli from a program which has been started by the DSN processor. This obsoletes the use of CAF and slighty simplifies the program you build the above code slice into. I have not tried this possibility myself. The example shows how to execute a DISPLAY command, but it works without any other changes than the command itself and the length of the command for all other DB2 commands. Please do not use the -STOP DB2 command. You can read a lot more about DSNWLI also known as the IFI interface in DB2 administration guide. The link is for DB2 version 8, but I am pretty confident it works for version 9 as well and I know it works for older versions. If you want to catch the output from a DB2 command returning more output than the size of return_area you have to read more in the manual, because I have not tried it.