Pages

Tuesday, May 11, 2021

Retrieve Job log information from SQL - IBM i

Job log

Job log provides information about the job including any errors and is extremely helpful to analyze what has happened. 

Sometimes, it becomes difficult to check all the messages in the job log (especially when the job is running for longer or if there are many messages in the job log) just by using DSPJOB/WRKJOB. 

SQL table function JOBLOG_INFO provides another way of accessing job log from SQL session. Each message from the job log is returned in a new row. This gives the flexibility to query and select the messages as required (E.g.: Requests, Diagnostic Informational etc). 

This table function requires one mandatory parameter Job name and one optional parameter Ignore errors. 

Job Name (JOB_NAME) - Qualified job name (job number/user/job name). For the job log of current job, '*' can be specified. 

Ignore Errors (IGNORE_ERRORS) - This parameter accepts either 'YES' or 'NO'. This determines what needs to be done in case of any error in Query execution. 
  • NO (default) - An error is returned. 
  • YES - A warning is returned. No rows are returned when an error is encountered. 
Let's have a quick look at a query to retrieve the job log of current session. 

1

2

3

4

5

6

7

8

SELECT ORDINAL_POSITION,

       MESSAGE_TYPE, 

       MESSAGE_TIMESTAMP, 

       FROM_LIBRARY, 

       FROM_PROGRAM, 

       MESSAGE_TEXT, 

       MESSAGE_SECOND_LEVEL_TEXT 

FROM TABLE(QSYS2/JOBLOG_INFO(JOB_NAME => '*'))


In the above query, 
  • Lines 1 - 7: Few columns from the table function JOBLOG_INFO. More info on each of these columns can be found below. 
  • Line - 8: JOB_NAME parameter is being passed with '*' to retrieve the job log of current job. 
Below are the few columns used in the above query. 

ORDINAL_POSITION - A unique number of each row in the job log. First message in the job log would contain '1' and incremented by '1' for the next message. 

MESSAGE_TYPE - Type of message from the job log. Below are the valid values for this column. 
  • COMMAND
  • COMPLETION
  • DIAGNOSTIC
  • ESCAPE
  • INFORMATIONAL
  • INQUIRY
  • NOTIFY
  • REPLY
  • REQUEST
  • SCOPE
  • SENDER
MESSAGE_TIMESTAMP - Timestamp when the message was written to Job log. 

FROM_LIBRARY - The library containing the program or service program that sent the message.

FROM_PROGRAM - The program or service program that sent the message.

MESSAGE_TEXT - First level of text message. 

MESSAGE_SECOND_LEVEL_TEXT - Second level of text message. 

There are many other useful columns present in this table function. Full list of columns can be found here.  

Above query returns all the messages in the job log. We can also select the records based on the message type or any other columns as required.

1

2

3

4

5

6

7

8

9

SELECT ORDINAL_POSITION,

       MESSAGE_TYPE, 

       MESSAGE_TIMESTAMP, 

       FROM_LIBRARY, 

       FROM_PROGRAM, 

       MESSAGE_TEXT, 

       MESSAGE_SECOND_LEVEL_TEXT 

FROM TABLE(QSYS2/JOBLOG_INFO(JOB_NAME => '*'))

WHERE  MESSAGE_TYPE = 'DIAGNOSTIC' ;


In the above query, 
  • Line - 9: We are only retrieving the diagnostic messages by adding condition on MESSAGE_TYPE. 
Job log info from SQL - IBM i

This query would only return the diagnostic messages along with it's position in the job log (ORDINAL_POSITION) and timestamp the message was sent to job log (MESSAGE_TIMESTAMP).

In both the queries mentioned above, we are only retrieving the job log of the current job by passing '*' against the job name. 

Let's have a look at retrieving the job log of other job. 

1

2

3

4

5

6

7

8

SELECT ORDINAL_POSITION,

       MESSAGE_TYPE, 

       MESSAGE_TIMESTAMP, 

       FROM_LIBRARY, 

       FROM_PROGRAM, 

       MESSAGE_TEXT, 

       MESSAGE_SECOND_LEVEL_TEXT 

FROM TABLE(QSYS2/JOBLOG_INFO(JOB_NAME => '730621/REDDYP/QPAD202846'))


In the above query, 
  • Lines 1 - 7: Columns from the table function as required. 
  • Line - 8: We are passing qualified job name with job number, user and job name. 
By adding condition on the columns we can extract the specific information required. 

E.g.: List of commands executed in an interactive session. 

This can be achieved by adding condition on MESSAGE_TYPE. 

1

2

3

4

5

6

7

8

SELECT MESSAGE_TYPE, 

       MESSAGE_TIMESTAMP, 

       FROM_LIBRARY, 

       FROM_PROGRAM, 

       MESSAGE_TEXT, 

       MESSAGE_SECOND_LEVEL_TEXT 

FROM TABLE(QSYS2/JOBLOG_INFO(JOB_NAME => '730621/REDDYP/QPAD202846'))

WHERE  MESSAGE_TYPE = 'REQUEST' 


Job log information from SQL - IBM i

Above query can be amended to fetch the first command or last command etc. 

For some reason, if the job doesn't present in the system or any other errors occurs while running the above query, an error is returned. This may cause an issue when running these query as part of procedures or function.

To avoid this, we could consider using optional parameter IGNORE_ERRORS (YES). By passing this parameter, JOBLOG_INFO would only return a warning and no records are returned. 

1

2

3

4

5

6

7

8

9

SELECT MESSAGE_TYPE, 

       MESSAGE_TIMESTAMP, 

       FROM_LIBRARY, 

       FROM_PROGRAM, 

       MESSAGE_TEXT, 

       MESSAGE_SECOND_LEVEL_TEXT 

FROM TABLE(QSYS2/JOBLOG_INFO(JOB_NAME => '730621/REDDYP/QPAD202846', IGNORE_ERRORS => 'YES'))

WHERE  MESSAGE_TYPE = 'REQUEST' 


If this parameter (IGNORE_ERRORS) is not passed, default value 'NO' is considered and an error is returned for any error while running the query. 


If you have any Suggestions or Feedback, Please leave a comment below or use Contact Form. 

Monday, May 3, 2021

Extract a portion of a Date/Time/Timestamp in RPGLE - IBM i

%SUBDT

Extracting Year, Month, Day, Hour, Minutes, Seconds or Milli seconds of a given Date/Time/Timestamp is required most of the times. 

This can be extracted easily by using %SUBDT. BIF name looks more similar to %SUBST which is used to extract a portion of string by passing from and two positions of the original string. Instead, We would need to pass a value (i.e., Date, Time or Timestamp ) and Unit (i.e., *YEARS, *MONTHS, *DAYS, *HOURS, *MINUTES, *SECONDS or *MSECONDS) to %SUBDT. 

Valid unit should be passed for the type of the value passed. Below are the valid values for each type.
  • Date - *DAYS, *MONTHS, *YEARS
  • Time - *HOURS, *MINUTES, *SECONDS
  • Timestamp - *DAYS, *MONTHS, *YEARS, *HOURS, *MINUTES, *SECONDS, *MSECONDS

Syntax:

%SUBDT(value : unit { : digits { : decpos} })

  • Value and Unit are the mandatory arguments. 
  • Digits and Decimal positions are optional and can only be used with *SECONDS for Timestamp.
We can either pass the full form for the unit or use the short form. Below is the mapping of Full form to short form. 
  • *YEARS - *Y
  • *MONTHS - *M
  • *DAYS - *D
  • *HOURS - *H
  • *MINUTES - *MN
  • *SECONDS - *S
  • *MSECONDS - *MS
Let's have a look at couple of simple examples to see how this works. 

E.g.:

Extract Date, Month and Year from a Date field. 

Extract Date, Month and Year from Date using %SUBDT - RPGLE

In the above example, 
  • We are using two different date formats *ISO & *DMY which shows different number of digits for year (*ISO - 4 digits and *DMY - 2 digits). However, %SUBDT would always return the full year (4 digits) irrespective of the format of the date passed. 
    • Line - 17: %SUBDT with *ISO date format. 
    • Line - 22: %SUBDT with *DMY date format. 
  • We don't necessarily pass the date in a variable. 
    • Lines - 17 & 22: We are passing the date variables to extract the year. 
    • Line - 27: We are passing %DATE() to extract the month from the current date. 
    • Line - 32: We are passing the required date to extract the day from the passed date. 

Let's have a look at another example to extract Hours, Minutes and Seconds from a Time. 

Extract Hours, Minutes and Seconds from Time using %SUBDT - RPGLE

In the above example, 
  • Line - 17: We are using *HOURS full form for value instead of short form *H. Using any of these two would return the same result. 
  • Either a variable with time, %TIME() or a time literal can be passed to %SUBDT. 
    • Line - 15: A time variable is being passed to extract hours. 
    • Line - 20: %TIME() is being passed to extract minutes. 
    • Line - 25: Specific time can be passed directly by using time literal 't'. We are extracting seconds from the time passed. 

Let's have a look at the final example to extract the milli seconds by passing the Timestamp. We will also explore the two optional parameters in this example. 

Extract Seconds and Milliseconds from Timestamp using %SUBDT - RPGLE

Extracting Years, Months, Days, Hours and Minutes from Timestamp is same as extracting from Date and Time. Milliseconds can be extracted from Timestamp and optional parameters can be used on seconds to specify how many decimal positions are to be extracted for milliseconds. 

In the above example, 
  • Line - 14: Extracting milliseconds from timestamp. 
  • Line - 19: Extracting seconds from timestamp along with milliseconds by specifying optional parameters digits and number of decimal positions. 
    • We are passing digits (total) '6' and decimal positions '4'. %SUBDT would only return milliseconds up to 4 digits along with seconds. wSeconds variable has been defined with 6 decimal positions, so the last two decimal positions would be stored as '00'. 
    • If the third parameter (digits) is specified, Fourth parameter (decimal positions) must be specified. And, Fourth parameter should always be 2 less than third parameter (2 digits for seconds).
    • If optional parameters aren't specified, no milliseconds would be returned. 


If you have any Suggestions or Feedback, Please leave a comment below or use Contact Form. 

Thursday, April 15, 2021

Return Number of Parameters (%PARMS) and Parameter Number (%PARMNUM) - IBM i

Return Number of Parameters (%PARMS)

Built-In Function %PARMS returns the number of parameters passed to the procedure where %PARMS is run. 

This BIF is useful when the procedure contains multiple optional parameters and if the procedure is called from different places with different number of parameters to identify the number of parameters passed. 

This is most commonly used to to handle the procedure logic based on the parameters passed only. 

Value returned by %PARMS is same as *PARMS in Program Status Data Structure if used in the main procedure. 

Let's have a look at the simple example to see how this works. 

%PARMS - RPGLE

In the above example, 
  • Lines - 5 to 9: Prototype declaration of the Sub procedure with one mandatory parameter, two optional parameters and one return value. We will see the code of procedure in a bit. 
  • Line - 18, 22 & 26: Calling the sub procedure with one mandatory parameter, two parameters and three parameters respectively. 
Let's have a look at the code for Sub procedure and see how the %PARMS work. 

%PARMS - RPGLE

In the above procedure, 
  • Lines 42 & 44: We are using %PARMS to determine the number of parameters passed. 
This is often used to handle the parameters (like initializing or making sure these aren't used in the logic etc) that are not passed and make sure the procedure works as expected. 

One thing to note here here, If the RTNPARM keyword is used return parameter would be considered as one of the (first) parameter and total number of parameters would be increased by 1. 

In the above example, If only one parameter is passed, %Parms would be 2 (by considering the return parameter). 

%PARMS with RTNPARM- RPGLE

In the above example, 
  • Line - 34: We are using RTNPARM keyword against the Procedure Interface. The same would need to mentioned in the Prototype as well. 
  • Lines - 42, 44 & 46: We are using %PARMS to check the number of parameters by considering the return parameter. 
    • %PARMS would be 2 if only one parameter is passed.
    • %PARMS would be 3 if two parameters are passed. 
    • %PARMS would be 4 if three parameters are passed. 

Return Parameter Number (%PARMNUM)

Built-In Function %PARMNUM returns the number of the parameter passed from the parameters list. 

Operand for this BIF should always be the parameter specified in the procedure interface. 

Below are the few important points to remember. 
  • %PARMNUM can only be used with the parameters defined in Procedure Interface (PI) and Parameter defined using PLIST cannot be used.
  • Parameter Name must be specified as it is defined in the PI. 
    • If the parameter is an array, Array should be used and not an Index. 
    • If the parameter is a data structure, Data structure should be used and not subfields.
    • If the parameter is a file, File should be used and not record format. 
  • If RTNPARM is used, Return value is considered as the first parameter and the other parameters would be considered next.
Let's have a look at an example without using RTNPARM first to understand how this works. 

PARMNUM - RPGLE

In the above example, 
  • Line - 34: Procedure Interface is defined without RTNPARM. 
  • Lines - 43, 46 & 49: %PARMNUM is used to get the parameter number passed. 
    • Only Parameter names from the Procedure Interface are to be used. 
  • Line - 52: %PARMS is used to return the total number of parameters passed. 
Similar to %PARMS, If we use RTNPARM total number of parameters would be increased by 1 and parameter number also would be increased by 1 as the return value is considered as first parameter. 

Let's add RTMPARM to the above procedure. 

PARMNUM with RTNPARM - RPGLE

By adding RTNPARM to the Procedure Interface, Number of the parameter and total number of parameters passed are increased by 1. 


If you have any Suggestions or Feedback, Please leave a comment below or use Contact Form. 

Popular Posts