Retrieve data from Data Queue:
Data Queues (DTAQ) has been very useful for communication between different Jobs on IBM i.
There are multiple APIs to work with Data Queues.
- Send Data Queue (QSNDDTAQ) - Write data to Data Queue
- Receive Data Queue (QRCVDTAQ) - Read data from Data Queue, This would clear the message from the Data Queue.
- Retrieve Data Queue Message (QMHRDQM) - Retrieve data from Data Queue, Multiple entries can be retrieved at once. This does not clear the retrieved messages from Data Queue.
In this post, we will see how to retrieve data from Data Queue in SQL using 'DATA_QUEUE_ENTRIES' Table function in QSYS2 with some examples. This would return the data similar to API 'QMHRDQM'.
E.g.:
Let's consider a Standard Data Queue created using below command with sequence 'FIFO'.
CRTDTAQ DTAQ(REDDYP1/TESTDTAQ) MAXLEN(20) SEQ(*FIFO) TEXT('Test Data Queue - FIFO')
- Line 1 - We are only retrieving ORDINAL_POSITION (Position of the row in result data set) and MESSAGE_DATA (Message Received from Data Queue) in this example. We will see full list of columns available towards the end.
- Line 2 - Table Function 'DATA_QUEUE_ENTRIES' is present in QSYS2. We don't always need to prefix it with library name if QSYS2 is present in SQL Path.
- Line 3 - Parameter 'DATA_QUEUE', Data Queue Name to be passed.
- Line 4 - Parameter 'DATA_QUEUE_LIBRARY, Library Name in which Data Queue is present is to be passed. Below are the other two special values allowed.
Let's use the same example and retrieve the data in reverse order.
This has returned the data in reverse order. If you notice, Ordinal Position hasn't changed because this would only represent how the data is retrieved in Result set and not how the data is present in DTAQ.
- Line 5 - Parameter 'SELECTION_TYPE' (Indicates how the messages are retrieved). This is an optional parameter with default value 'ALL'. Passing 'REVERSE' would return the data in reverse order. Below are some of the allowed values.
Let's look at another example with a Keyed Data Queue.
E.g.:
Create a standard Data Queue with KEYED sequence with Key length of '2' using the below command.
CRTDTAQ DTAQ(REDDYP1/KEYEDDTAQ) MAXLEN(20) SEQ(3KEYED) KEYLEN(2) TEXT('Test Data Queue - Keyed')
Data sent to data queue in the different order than the Keyed sequence. However, Data is retrieved in Keyed Sequence. To Retrieve the data without any Key comparison, Just passing Data Queue Name and Library Name should be sufficient.
- Line 1 - Retrieving another column 'KEY_DATA' for Keyed Data Queue.
In the above example, Third Entry has been written with Key Data '04' and Fourth Entry has been written with Key Data '03'.
When the data is retrieved, Data is retrieved in the order of Key rather than the sequence data is entered into.
Let's use the Selection Type, Key data and Key Order to retrieve the data based on the Key data passed.
- Line 5 - Value 'KEY' is passed against the 'SELECTION_TYPE' to indicate the data to be retrieved based on the Key passed.
- Line 6 - Key data to be passed against 'KEY_DATA' parameter. We have passed '02' in this example.
- Line 7 - Comparison criteria to be passed against KEY_ORDER. We have passed 'GE' (Greater than or Equal To) in this example and all messages with Key data greater than or equal to '02' is retrieved. Below are the other values that can be passed to this parameter.
We have only retrieved columns ORDINAL_POSITION, MESSAGE_DATA and KEY_DATA in our examples above. Below are the other columns this function would return.
No comments:
Post a Comment