Now, Get RPG Codes in all three formats only on this website on every RPGLE related programming article.

Fixed, /Free and Fully Free RPG


Write data to data queue using QSNDDTAQ API in RPG AS400

Write data to data queue using QSNDDTAQ api in RPG AS400
Write data to data queue using QSNDDTAQ api in RPG AS400, Data queue, QSNDDTAQ, api as400, send data queue api, rpg, introduction, about, as400,ibmi
Write data to data queue using QSNDDTAQ api in RPG AS400

Send Data Queue (QSNDDTAQ) API

The Send Data Queue API will send the data to the specified data queue.

When an entry is sent to a standard data queue, the storage is allocated for each entry i.e. the value specified for the maximum entry length on the Create Data Queue (CRTDTAQ) command.

Please note that you can also use this API to send data to the DDM data queue (present on the remote system).

Technically speaking, when a Job uses this API a cache is created to allow faster access to the Data queue.

Required Parameters

  1. Data Queue Name: Input parm and Char(10)
  2. Library Name: Input parm and Char(10)
  3. Length of Data: Input parm and Packed(5,0)
  4. Data: Input parm and Char(*)

There are some optional parameters as well which are not discussed here in this blog.

Coding QSNDDTAQ API in RPGLE Fixed, /Free and Fully Free format

Writing RPGLE code to send/write data to the data queue passed as an Input parameter to the program

RPG Code in Fixed format for Writing data to data queue using QSNDDTAQ API in RPG AS400
     D main            pr                  extpgm('EXTPGM5')              
     D                               10a                                  
     D                               10a                                  
     D                               20a                                  
     D main            pi                                                 
     D dtquenam                      10a                                  
     D dtquelib                      10a                                  
     D data                          20a                                  
     D length          s              5p 0                                
                                                                          
     C                   EVAL      length = %len(%trim(data))             
     C                   CALL      'QSNDDTAQ'                             
     C                   PARM                    dtquenam                 
     C                   PARM                    dtquelib                 
     C                   PARM                    length                   
     C                   PARM                    data                     
     C                   SETON                                        LR  
     C                   RETURN                                                                                         
RPG Code in /Free format for Writing data to data queue using QSNDDTAQ API in RPG AS400
     D QSNDDTAQ        PR                  extpgm('QSNDDTAQ')       
     D dtquenam                      10A   const                    
     D dtquelib                      10A   const                    
     D datalen                        5P 0 const                    
     D databuf                    32767A   const options(*varsize)  
                                                                    
     D main            pr                  extpgm('EXTPGM5')        
     D                               10a                            
     D                               10a                            
     D                               20a                            
     D main            pi                                           
     D dtquenam                      10a                            
     D dtquelib                      10a                            
     D data                          20a                            
      /Free                                                         
        QSNDDTAQ(dtquenam:dtquelib:%len(%trim(data)):data);         
                                                                    
        *INLR = *ON;                                                
        return;                                                     
      /End-Free                                                                                                                                
RPG Code in fully Free format for Writing data to data queue using QSNDDTAQ API in RPG AS400
**FREE                                              
dcl-pr QSNDDTAQ extpgm ;                            
  *n char(10) const;                                
  *n char(10) const;                                
  *n packed(5:0) const;                             
  *n char(32767) const options(*varsize);           
end-pr;                                             
                                                    
dcl-pi *N;                                          
  dtquenam char(10);                                
  dtquelib char(10);                                
  data char(20);                                    
end-pi;                                             
                                                    
QSNDDTAQ(dtquenam:dtquelib:%len(%trim(data)):data); 
                                                    
*INLR = *ON;                                        
return;                                                                                                             

Compile the program EXTPGM5 using option 14 against the SQLRPGLE source member or using the command CRTSQLRPGI.

Execute the QSNDDTAQ code

For testing the above code, we need a data queue to be passed to this program. So, Let's first create a data queue

Create Data Queue named DATAQUEUE1

Use the command CRTDTAQ to create the standard data queue named DATAQUEUE1 of MAXLEN 5000.

                          Create Data Queue (CRTDTAQ)                         
                                                                              
Type choices, press Enter.                                                    
                                                                              
Data queue . . . . . . . . . . . DTAQ         > DATAQUEUE1                    
  Library  . . . . . . . . . . .              >   EASYCLASS1                  
Type . . . . . . . . . . . . . . TYPE           *STD                          
Maximum entry length . . . . . . MAXLEN       > 5000                          
Force to auxiliary storage . . . FORCE          *NO                           
Sequence . . . . . . . . . . . . SEQ            *FIFO                         
Include sender ID  . . . . . . . SENDERID       *NO                           
Queue size:                      SIZE                                         
  Maximum number of entries  . .                *MAX16MB                      
  Initial number of entries  . .                16                            
Automatic reclaim  . . . . . . . AUTORCL        *NO                           
Text 'description' . . . . . . . TEXT           Test Data queue                                                                                                      

Data queue named DATAQUEUE1 was created and we can see its object using the WRKOBJ DATAQUEUE1 command on the AS400 command line.

                              Work with Objects                       
                                                                      
Type options, press Enter.                                            
  2=Edit authority        3=Copy   4=Delete   5=Display authority   7=
  8=Display description   13=Change description                       
                                                                      
Opt  Object      Type      Library     Attribute   Text               
     DATAQUEUE1  *DTAQ     EASYCLASS1              Test Data queue    
                                                                      
                                                                      
                                                                      
                                                                      
                                                                      
                                                                      
                                                                      
                                                                      
                                                                      
                                                                      
                                                                      
Parameters for options 5, 7 and 13 or command                         
===> wrkobj dataqueue1                                                
F3=Exit   F4=Prompt   F5=Refresh   F9=Retrieve   F11=Display names and

Call RPGLE program using QSNDDTAQ from the command line

CALL PGM(EASYCLASS1/EXTPGM5)                    
     PARM('DATAQUEUE1' 'EASYCLASS1' 'HELLO WORLD')

Once you call this program the entry 'HELLO WORLD' will be placed in the standard data queue DATAQUEUE1.

Program Output:Display Data Queue entry using SQL UDTF

Run below SQL UDTF to get entries of specified data queue using QSYS2.DATA_QUEUE_ENTRIES user-defined table function.

SELECT ORDINAL_POSITION, CAST(MESSAGE_DATA AS CHAR(50)) 
   FROM TABLE(QSYS2.DATA_QUEUE_ENTRIES(
        DATA_QUEUE => 'DATAQUEUE1',
        DATA_QUEUE_LIBRARY => 'EASYCLASS1'))

Data queue entry

   ORDIN00001   CAST function  
            1   HELLO WORLD    
********  End of data  ********

Related Post

Data Queue in AS400
Read data from data queue using QRCVDTAQ api in RPG AS400

Post a Comment

© AS400 and SQL Tricks. All rights reserved. Developed by Jago Desain