Using IBM i? Need to create Excel, CSV, HTML, JSON, PDF, SPOOL reports? Learn more about the fastest and least expensive tool for the job: SQL iQuery.
[ NOTE: CHKOBJ() has been deprecated. The OBJEXISTS() UDF is preferred over CHKOBJ(). We shall continue to ship CHKOBJ() and evetually it will become a wrapper for OBJEXISTS(). ]
The iQuery.objExists() UDF checks to see if the object exists. If it exists, the UDF returns 1, otherwise it returns 0.
Note: This function uses our polymorphic qualified name syntax.
iQuery.objExists( 'library-name', 'object-name', 'object-type' = *FILE, quietMode = 1);
iQuery.objExists( 'object-name', 'object-type' = *FILE, quietMode = 1);
The library-name is the name of the library that contains the object-name specified on the second parameter. This parameter may be a library name, or a special value including *LIBL and *CURLIB. Long SQL names are supported.
The object-name is the name of the object to be checked for existence. SQL long names are supported. In form 2, the object name may be a fully qualified object name (e.g., qgpl/qrpglesrc) or just the object name (unqualified).
The object-type is the IBM i system object type, such as *FILE, *PGM, *LIB, *DTAARA, etc. with or without the leading asterisk. If unspecified, the default is *FILE.
The quoteMode controls whether to generate an Object Not Found message in the joblog. The original SQL iQuery CHKOBJ() UDF defaulted to 0, which meant users who processed long lists of object were seeing lots of "Object Not Found" messages in their joblog. The default for OBJEXISTS() is to run in quite mode and not log the not found message. To log the Not Found message, specify 0 for this parameter, to avoid logging the Not Found message, specify 1 for this parameter, this is the default.
The return value is 1 if the object exists, otherwise 0 is returned.
dcl-s exists int(10);
exec sql values iQuery.objExists('SALESLIB', 'ORDERSUM') into :EXISTS;
if (exists = 1); // Does the file exist?
exec SQL drop table saleslib.ordersum;
endif;
dcl-s exists int(10);
dcl-s nextOrder varchar(7);
exec sql values iQuery.objExists('SALESLIB/ORDERSEQ','*dtaara') into :EXISTS;
if (exists = 1); // Does the data area exist?
exec SQL select value INTO :nextOrder
FROM table(iQuery.dtaara('SALESLIB/ORDERSEQ')) da;
endif;