MOVSQLFBQ - moves spoll files between output queues

By kimot, 24 December, 2022

If you need to move bunch of spool files between output queues and don't have (or cannot use - due to BRM2270) MOVSPLFBRM, this piece of code can do the job for you.

The program runs with these parameters: OLD_LIBRARY / OLD_OUTPUT_QUEUE / USER / SPOOL_FILE_NAME / USER_DATA / NEW_LIBRARY / NEW_QUEUE

/******************************************************************************/     
/* Program moves spool files between output queues                            */     
/* Parameters: old_lib/old_queue/user/filename/user_data/new_lib/new_queue    */     
/* V 1.0  cTomik 02/2021                                                      */     
/* SQL QSYS2.OUTPUT_QUEUE_ENTRIES view used                                   */     
/******************************************************************************/     
                                                                                     
             PGM        PARM(&OLD_LIB &OLD_OUTQ &USER &SPOOL_NAME +                  
                          &USER_DATA &NEW_LIB &NEW_OUTQ)                             
             DCLF       FILE(*LIBL/TMP_SQL_PF) OPNID(FILE) ALWNULL(*YES)             
             DCL        VAR(&OLD_LIB) TYPE(*CHAR) LEN(10)                            
             DCL        VAR(&OLD_OUTQ) TYPE(*CHAR) LEN(10)                           
             DCL        VAR(&NEW_LIB) TYPE(*CHAR) LEN(10)                            
             DCL        VAR(&NEW_OUTQ) TYPE(*CHAR) LEN(10)                           
             DCL        VAR(&USER)  TYPE(*CHAR)  LEN(10)                             
             DCL        VAR(&SPOOL_NAME)  TYPE(*CHAR)  LEN(10)                       
             DCL        VAR(&USER_DATA)  TYPE(*CHAR)  LEN(10)                        
             DCL        VAR(&MESSAGE) TYPE(*CHAR) LEN(100)                           
             DCL        VAR(&CMD) TYPE(*CHAR) LEN(500)                               
             DCL        VAR(&SELECT) TYPE(*CHAR) LEN(500) VALUE(' ')                 
             DCL        VAR(&QUOTE) TYPE(*CHAR) LEN(1) VALUE('''')                   
             DCL        VAR(&S_J_NBR)  TYPE(*CHAR)  LEN(6)                           
             DCL        VAR(&S_J_NAM)  TYPE(*CHAR)  LEN(10)                          
             DCL        VAR(&S_J_POS) TYPE(*INT)                                     
             DCL        VAR(&S_J_USR)  TYPE(*CHAR)  LEN(10)                          
             DCL        VAR(&S_C_DAT)  TYPE(*CHAR)  LEN(8)                           
             DCL        VAR(&S_C_TIM)  TYPE(*CHAR)  LEN(6)                           
             DCL        VAR(&S_S_NAM)  TYPE(*CHAR)  LEN(10)                          
             DCL        VAR(&ROWCOUNT) TYPE(*INT) VALUE(0)                           
             DCL        VAR(&SPOOL_CNT)  TYPE(*CHAR)  LEN(10)                        
                                                                                    
          /* Drop temporary table           */                                      
             CHGVAR     VAR(&CMD) VALUE('DROP TABLE QTEMP.TMP_SQL_PF')               
             QSYS/RUNSQL SQL(&CMD) COMMIT(*NONE)                                     
             MONMSG MSGID(SQL9010)                                                   
                                                                                    
          /* Temporary table for values exchange */                                 
             CHGVAR     VAR(&CMD) VALUE('CREATE TABLE QTEMP.TMP_SQL_PF ( +           
                                         OUTQLIB   char(10), +                       
                                         OUTQ      char(10), +                       
                                         SPOOLNAME char(10), +                       
                                         USER_NAME char(10), +                       
                                         USER_DATA char(10), +                       
                                         JOB_NAME  char(28), +                       
                                         CREATED   CHAR(26) )')                      
             QSYS/RUNSQL SQL(&CMD) COMMIT(*NONE)                                     
             MONMSG MSGID(SQL9010)                                                   
                                                                                     
           /* Temporary table in QTEMP is used    */                                 
             OVRDBF FILE(TMP_SQL_PF) TOFILE(QTEMP/TMP_SQL_PF) SECURE(*YES)           
                                                                                     
           /* Output queues existence is checked   */                                
             CHKOBJ     OBJ(&OLD_LIB/&OLD_OUTQ) OBJTYPE(*OUTQ)                       
             MONMSG     MSGID(CPF9801 CPF9810 CPF0001) EXEC(DO)                      
             CHGVAR   VAR(&MESSAGE) VALUE('Queue (From) doesn''t exist: ' +          
                *CAT %TRIM(&OLD_LIB) *CAT '/' *CAT %TRIM(&OLD_OUTQ) *CAT ' !!!')     
               SNDPGMMSG  MSG(&MESSAGE)                                              
               GOTO       CMDLBL(END)                                                
             ENDDO                                                                   
                                                                                     
             CHKOBJ     OBJ(&NEW_LIB/&NEW_OUTQ) OBJTYPE(*OUTQ)                       
             MONMSG     MSGID(CPF9801 CPF9810 CPF0001) EXEC(DO)                      
             CHGVAR   VAR(&MESSAGE) VALUE('Queue (To) doesn''t exist: ' +            
                *CAT %TRIM(&NEW_LIB) *CAT '/' *CAT %TRIM(&NEW_OUTQ) *CAT ' !!!')     
               SNDPGMMSG  MSG(&MESSAGE)                                              
               GOTO       CMDLBL(END)                                                
             ENDDO                                                                   
                                                                                     
            /* Create SQL request for demanded spool files  */                       
             IF COND(%TRIM(&USER) *EQ ' ') THEN(CHGVAR +                             
               VAR(&USER) VALUE('-'))                                                
             ELSE CMD(CHGVAR VAR(&SELECT) VALUE( %TRIM(&SELECT) *CAT +               
             ' AND USER_NAME = ' *CAT &QUOTE *CAT &USER *TCAT &QUOTE ))              
                                                                                     
             IF COND(%TRIM(&USER_DATA) *EQ ' ') THEN(CHGVAR +                        
               VAR(&USER_DATA) VALUE('-'))                                           
             ELSE CMD(CHGVAR VAR(&SELECT) VALUE( %TRIM(&SELECT) *CAT +               
             ' AND USER_DATA = ' *CAT &QUOTE *CAT &USER_DATA *TCAT &QUOTE ))         
                                                                                     
             IF COND(%TRIM(&SPOOL_NAME) *EQ ' ') THEN(CHGVAR +                       
               VAR(&SPOOL_NAME) VALUE('-'))                                          
             ELSE CMD(CHGVAR VAR(&SELECT) VALUE( %TRIM(&SELECT) *CAT +               
             ' AND SPOOLNAME = ' *CAT &QUOTE *CAT &SPOOL_NAME *TCAT &QUOTE ))        
                                                                                     
             CHGVAR     VAR(&CMD) VALUE('INSERT INTO TMP_SQL_PF +                    
              (SELECT OUTQLIB, OUTQ, SPOOLNAME, USER_NAME, USER_DATA, +              
              JOB_NAME, CREATED FROM QSYS2.OUTPUT_QUEUE_ENTRIES +                    
              WHERE OUTPUT_QUEUE_LIBRARY_NAME = ' *CAT &QUOTE *CAT &OLD_LIB +        
              *TCAT &QUOTE *CAT ' AND OUTPUT_QUEUE_NAME = ' *CAT &QUOTE *CAT +       
             &OLD_OUTQ *TCAT &QUOTE *CAT ' ' *TCAT &SELECT *TCAT ')')                
             QSYS/RUNSQL SQL(&CMD) COMMIT(*NONE)                                     
             MONMSG MSGID(SQL9010)                                                   
                                                                                     
             CHGVAR VAR(&MESSAGE) VALUE('Moooving: ' *CAT %TRIM(&OLD_LIB)   +        
              *CAT '/' *CAT %TRIM(&OLD_OUTQ) *CAT '/' *CAT %TRIM(&USER) +            
              *CAT '/' *CAT %TRIM(&SPOOL_NAME) *CAT '/' *CAT %TRIM(&USER_DATA))      
             SNDPGMMSG  MSG(&MESSAGE)                                                
                                                                                     
           /* CHGSPLFA for selected spooled files          */                        
LOOP:        RCVF       OPNID(FILE)                                                  
             MONMSG     MSGID(CPF0864) EXEC(GOTO CMDLBL(END))                        
             CHGVAR     VAR(&ROWCOUNT) VALUE(&ROWCOUNT + 1)                          
             CHGVAR     VAR(&S_S_NAM) VALUE(&FILE_SPOOLNAME)                         
             CHGVAR     VAR(&S_J_NBR) VALUE(%SST(&FILE_JOB_NAME 1 6))                
             CHGVAR     VAR(&S_J_USR) VALUE(&FILE_USER_NAME)                         
             CHGVAR     VAR(&S_J_POS) VALUE(%SCAN('/' +                              
                          &FILE_JOB_NAME 8) + 1)                                     
             CHGVAR     VAR(&S_J_NAM) VALUE(%SST(&FILE_JOB_NAME +                    
                          &S_J_POS 10))                                              
             CHGVAR     VAR(&S_C_DAT) VALUE(%SST(&FILE_CREATED 9 2) +                
                          *TCAT                                       -              
                        %SST(&FILE_CREATED 6 2) +                                    
                          *TCAT                                       -              
                        %SST(&FILE_CREATED 1 4))                                     
             CHGVAR     VAR(&S_C_TIM) VALUE(%SST(&FILE_CREATED 12 +                  
                          2) +                                                       
                          *TCAT                                       -              
                        %SST(&FILE_CREATED 15 2) +                                   
                          *TCAT                                       -              
                        %SST(&FILE_CREATED 18 2))                                    
             CHGSPLFA   FILE(&S_S_NAM) +                                             
                          JOB(&S_J_NBR/&S_J_USR/&S_J_NAM) +                          
                          CRTDATE(&S_C_DAT &S_C_TIM) +                               
                          OUTQ(&NEW_LIB/&NEW_OUTQ)                                   
             GOTO       CMDLBL(LOOP)                                                 
                                                                                     
END:                                                                                 
             CHGVAR     VAR(&SPOOL_CNT) VALUE(%CHAR(&ROWCOUNT))                      
             CHGVAR     VAR(&MESSAGE) VALUE(%TRIM(&SPOOL_CNT) *CAT +                 
                          ' - spool files moved to ' *CAT +                          
                          %TRIM(&NEW_LIB) *CAT '/' *CAT +                            
                          %TRIM(&NEW_OUTQ))                                          
             SNDPGMMSG  MSG(&MESSAGE)                                                
             ENDPGM                                                                  

Before compilation you should be sure you have temporary PF (TMP_SQL_PF) available somewhere in your liblist.

You can make CMD which will collect input parameters for you too :

*************** Beginning of data ***************************************************
             CMD        PROMPT('Move Spool Files Between OutQ')                      
             PARM       KWD(OLD_LIB) TYPE(*CHAR) LEN(10) RSTD(*NO) +                 
                          DFT(' ') PROMPT('From OUTQ library : ')                    
             PARM       KWD(OLD_OUTQ) TYPE(*CHAR) LEN(10) RSTD(*NO) +                
                          DFT(' ') PROMPT('From OUTQ name : ')                       
             PARM       KWD(USER) TYPE(*CHAR) LEN(10) RSTD(*NO) +                    
                          DFT(' ') PROMPT('Only for this User : ')                   
             PARM       KWD(SPOOL_NAME) TYPE(*CHAR) LEN(10) +                        
                          RSTD(*NO) DFT(' ') PROMPT('Only this File +                
                          name : ')                                                  
             PARM       KWD(USER_DATA) TYPE(*CHAR) LEN(10) RSTD(*NO) +               
                          DFT(' ') PROMPT('Only this User Data : ')                  
             PARM       KWD(NEW_LIB) TYPE(*CHAR) LEN(10) RSTD(*NO) +                 
                          DFT(' ') PROMPT('To OUTQ library : ')                      
             PARM       KWD(NEW_OUTQ) TYPE(*CHAR) LEN(10) RSTD(*NO) +                
                          DFT(' ') PROMPT('To OUTQ name : ')                        

CMD screen will look like this :

                   Move Spool Files Between OutQ (MOVSPLFBQ)      
                                                                   
 Type choices, press Enter.                                        
                                                                   
 From OUTQ library :  . . . . . .   ' '           Character value  
 From OUTQ name :   . . . . . . .   ' '           Character value  
 Only for this User :   . . . . .   ' '           Character value  
 Only this File name :  . . . . .   ' '           Character value  
 Only this User Data :  . . . . .   ' '           Character value  
 To OUTQ library :  . . . . . . .   ' '           Character value  
 To OUTQ name :   . . . . . . . .   ' '           Character value