MainframeSupports
tip week 45/2009:

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:

DCL dsnwli EXTERNAL ENTRY OPTIONS(ASM INTER RETCODE);
DCL function CHAR(8);
DCL 01 ifca,
    02 lngth FIXED BIN(15),
    02 unused FIXED BIN(15),
    02 eye_catcher CHAR(4),
    02 owner_id CHAR(4),
    02 ifcarc1 FIXED BIN(31),
    02 ifcarc2 FIXED BIN(31),
    02 bytes_moved FIXED BIN(31),
    02 excess_recds FIXED BIN(31),
    02 opn_writ_seq_num FIXED BIN(31),
    02 num_recds_lost FIXED BIN(31),
    02 opn_name_for_reada CHAR(4),
    02 opn_names_area,
    03 opn_lngth FIXED BIN(15),
    03 unused_2 FIXED BIN(15),
    03 array_opn_names(8) CHAR(4),
    02 trace_nos_area,
    03 trace_lngth FIXED BIN(15),
    03 unused_3 FIXED BIN(15),
    03 array_trace_nos(8) CHAR(2),
    02 diagnos_area,
    03 diagnos_lngth FIXED BIN(15),
    03 unused_4 FIXED BIN(15),
    03 diagnos_data CHAR(80);
DCL 01 output_area,
    02 lngth FIXED BIN(15),
    02 unused FIXED BIN(15),
    02 text_or_command CHAR(254);
DCL 01 return_area,
    02 lngth FIXED BIN(31),
    02 rtrn_buff CHAR(4096);

function = 'COMMAND';
ifca = '';
ifca.lngth = storage(ifca);
ifca.eye_catcher = 'IFCA';
ifca.ower_id = 'LOC2';

return_area.rtrn_buff = '';
return_area.lngth = 4096;

output_area.unused = 0;
output_area.lngth = 4 + 11;
output_area.text_or_command = '-DIS GROUP ';

CALL dsnwli(function, ifca, return_area, output_area);

PUT SKIP LIST('IFI RETURN CODE=' !! ifca.ifcarc1);
PUT SKIP LIST('IFI REASON CODE=' !! Ifca.ifcarc2);
PUT SKIP DATA(return_area);

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.

Previous tip in english        Sidste danske tip        Tip list