SQLRPGLE: Getting a list of objects in a library
With the use of QCMDEXC, DSPOBJD and SQL you are able to easily retrive a list of libraries, PFs, LFs.. a lot of different object types. Here's how to do that (get a list of objects in a library) with embedded SQL.
Firstly, declare this EXTPGM prototype so we are able to run commands:
Dcl-PR Run ExtPgm('QCMDEXC'); Command Char(200) Const; Length Packed(15:5) Const; END-PR;
The next part is to run the DSPOBJD command and then put the results into an outfile. Note that I put it into a monitor because it will 'crash' if there are no objects in the library:
Monitor; Run( 'DSPOBJD ' + 'OBJ(#LALLAN/*ALL) ' + 'OBJTYPE(*ALL) ' + 'OUTPUT(*OUTFILE) OUTFILE(QTEMP/OBJLST) ' :80); On-Error *All; Return; Endmon;
You'll need a data structure to store the current record for the cursor, so for my example:
Dcl-Ds CurOBJ Qualified; Object Char(10); Description Char(50); END-DS;
The last part is using embedded SQL to query our outfile, which is simple:
Query = 'SELECT ' + 'ODOBNM, ' //Object name + 'ODOBTX ' //Description + 'FROM QTEMP/OBJLST '; //The outfile EXEC SQL PREPARE TheQuery FROM :Query; EXEC SQL DECLARE TheCursor CURSOR FOR TheQuery; EXEC SQL OPEN TheCursor; If (SQLSTATE >= '02000'); //Error? Else; EXEC SQL FETCH TheCursor INTO :CurOBJ; Dow (SQLSTATE = '00000'); DSPLY CurOBJ.Description; EXEC SQL FETCH TheCursor INTO :CurOBJ; ENDDO; Endif; EXEC SQL CLOSE TheCursor;
Only found this out recently so I had to write about it ASAP - seems a little hacky having to use a command to get this data but it's pretty cool IMO.
Edit: Thanks to @PHPDave, he sent me this code snippet so you don't even have to use DSPOBJD - so you'd just have to replace your select (& update the fields) with the following statement:
SELECT * FROM TABLE (QSYS2.OBJECT_STATISTICS('#LALLAN','ALL') ) AS OBJECTS_IN_LIBRARY ORDER BY OBJCREATED
You can read more about OBJECT_STATISTICS here.