External stored procedure that writes to a data queue

External stored procedure that writes to a data queue.
External stored procedure that writes to a data queue, SQL statements, RPGLE program for External stored procedure, CREATE PROCEDURE, MODIFIES SQL DATA, GENERAL parameter, EXECUTE IMMEDIATE statement, ibmi, as400 and sql tricks, as400 tutorial, ibmi tutorial
External stored procedure that writes to a data queue

What is Data Queue?

Data queue is the type of system object that one can create to which a program or procedure can send data and from which another program or procedure can receive data.

How to create a stored procedure to write a Data Queue

First, we need a program that writes the data to the data queue. For example;

      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                                                                              

Explanation of the above code

      D QSNDDTAQ        PR                  extpgm('QSNDDTAQ')                       
      D dtquenam                      10A   const                                    
      D dtquelib                      10A   const                                    
      D datalen                        5P 0 const                                    
      D databuf                    32767A   const options(*varsize)                  
  • Here, The Send Data Queue (QSNDDTAQ) API is used to send the data to the data queue and this API has several parameters such as name of the data queue dtquenam, then we need to provide the library dtquelib, length of the data queue datalen and data buffer databuf. So, these are all parameters which we need to send data to the data queue.
  •       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                                                                              
    
  • Here, we deifned procedure prototype and procedure interface for the main program(replacement for *entry parameter list). We are taking inputs Data queue name dtquenam, Data queue library dtquelib and Data then we are calling the API QSNDDTAQ passing these inputs.
  • Example of an External stored procedure that writes to a data queue

          D sqlproc         S            500a   inz(*blanks)                             
           /Free                                                                         
            sqlproc = 'CREATE PROCEDURE EASYCLASS1.WRITETODATAQUEUE( ' +                 
                      'IN DQNAM CHAR(10), IN DQLIB CHAR(10), ' +                         
                      'IN DATA CHAR(20)) ' +                                             
                      'LANGUAGE RPGLE ' +                                                
                      'SPECIFIC EASYCLASS1.EXTPROCED5 ' +                                
                      'EXTERNAL NAME EASYCLASS1.EXTPGM5 ' +                              
                      'NOT DETERMINISTIC ' +                                             
                      'NO SQL ' +                                                        
                      'CALLED ON NULL INPUT ' +                                          
                      'PARAMETER STYLE GENERAL';                                         
                                                                                         
            EXEC SQL                                                                     
            EXECUTE IMMEDIATE :sqlproc;                                                  
                                                                                         
             *INLR = *ON;                                                                
           /End-Free                                                                     
    

    Explanation of the above code

          D sqlproc         S            500a   inz(*blanks)                             
           /Free                                                                         
            sqlproc = 'CREATE PROCEDURE EASYCLASS1.WRITETODATAQUEUE( ' +                 
                      'IN DQNAM CHAR(10), IN DQLIB CHAR(10), ' +                         
                      'IN DATA CHAR(20)) ' +                                             
                      'LANGUAGE RPGLE ' +                                                
    
  • Here, we have defined a variable sqlproc of 500 characters initialized with blanks and then we create a procedure named WRITETODATAQUEUE which is going to create in library EASYCLASS1 with input parameters IN DQNAM CHAR(10) IN DQLIB CHAR(10) and IN DATA CHAR(20) with language RPGLE.
  •                   'SPECIFIC EASYCLASS1.EXTPROCED5 ' +                              
  • Here, the specific name is EXTPROCED5 every stored procedure has a specific name that is different from all others, and the stored procedure must be unique in its existence.
  •                   'EXTERNAL NAME EASYCLASS1.EXTPGM5 ' +               
  • Here, We create an external procedure that calls the program named EXTPGM5 which is present in the library EASYCLASS1.
  •              'NOT DETERMINISTIC ' +                                             
                      'NO SQL ' +                                                        
                      'CALLED ON NULL INPUT ' +                                          
                      'PARAMETER STYLE GENERAL';                                         
    
    
  • Here, NOT DETERMINISTIC function always returns the same results if we are given the same input values from the data queue and its default. Then NO SQL indicates that we are not using any SQL and CALLED ON NULL INPUT indicates that the function will be called normally when some of its arguments are null. Then using general parameter style means the parameters are passed as they are defined in the stored procedure creation script.
  • Calling the external stored procedure in the RPGLE program

          D parm1           s             10a   inz('DATAQUEUE1')                        
          D parm2           s             10a   inz('EASYCLASS1')                        
          D parm3           s             20a   inz('TESTDATA')                          
           /Free                                                                         
            EXEC SQL                                                                     
            CALL EASYCLASS1.WRITETODATAQUEUE(:parm1,:parm2,:parm3);                      
             *INLR = *ON;                                                                
           /End-Free                                                                     
    
  • Here, we need to call an external stored procedure so we can call using SQL CALL then we have to write EXEC SQL and write CALL EASYCLASS1.WRITETODATAQUEUE(:parm1,:parm2,:parm3) where we have passing three parms parm1, parm2 and parm3.
  • Post a Comment

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